Python - How do I call class method from another newly modified python file












2















I'm new in python programming and would like to seek help in calling a method from another newly modified python file.



I currently have 3 python files: main.py, addBody.py, and originalBody.py.



The addBody will add one segment of the body to originalBody.py in each iteration. This is done by read and write function.



For example,



in the first iteration (initiated from main.py), originalBody.py will look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body


in second iteration, originalBody.py will automatically look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
object2 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body
bodySegment[1] = 2 #two body


When I'm trying to call the class from main.py,



main.py



from originalBody import BODY

for i in range (0,10):

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = BODY()
print(parent.bodySegment)


in the first iteration it runs smoothly:



{0: 1}


In the second iteration, after adding the body segment to originalBody.py, supposedly the result is expected to print out:



{0: 1, 1: 2}


However, it prints out the result from the first iteration:



{0: 1}


I suspect that my code calls the function from the original version of originalBody.py that has not modified yet. Do I need to add in some code in my main to get the class method from the modified version of originalBody? Or do I need to add in time delay to call the method right after the modification of originalBody?










share|improve this question























  • Seems you may want to reload the file? Did you look into imp---particularly load_source?

    – mattsap
    Nov 21 '18 at 15:58











  • So you modify your already loaded code at runtime? Why?

    – MisterMiyagi
    Nov 21 '18 at 16:09











  • @mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

    – Nadiah Naz
    Nov 21 '18 at 16:37











  • @MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

    – Nadiah Naz
    Nov 21 '18 at 16:40











  • It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

    – MisterMiyagi
    Nov 21 '18 at 16:47
















2















I'm new in python programming and would like to seek help in calling a method from another newly modified python file.



I currently have 3 python files: main.py, addBody.py, and originalBody.py.



The addBody will add one segment of the body to originalBody.py in each iteration. This is done by read and write function.



For example,



in the first iteration (initiated from main.py), originalBody.py will look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body


in second iteration, originalBody.py will automatically look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
object2 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body
bodySegment[1] = 2 #two body


When I'm trying to call the class from main.py,



main.py



from originalBody import BODY

for i in range (0,10):

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = BODY()
print(parent.bodySegment)


in the first iteration it runs smoothly:



{0: 1}


In the second iteration, after adding the body segment to originalBody.py, supposedly the result is expected to print out:



{0: 1, 1: 2}


However, it prints out the result from the first iteration:



{0: 1}


I suspect that my code calls the function from the original version of originalBody.py that has not modified yet. Do I need to add in some code in my main to get the class method from the modified version of originalBody? Or do I need to add in time delay to call the method right after the modification of originalBody?










share|improve this question























  • Seems you may want to reload the file? Did you look into imp---particularly load_source?

    – mattsap
    Nov 21 '18 at 15:58











  • So you modify your already loaded code at runtime? Why?

    – MisterMiyagi
    Nov 21 '18 at 16:09











  • @mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

    – Nadiah Naz
    Nov 21 '18 at 16:37











  • @MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

    – Nadiah Naz
    Nov 21 '18 at 16:40











  • It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

    – MisterMiyagi
    Nov 21 '18 at 16:47














2












2








2








I'm new in python programming and would like to seek help in calling a method from another newly modified python file.



I currently have 3 python files: main.py, addBody.py, and originalBody.py.



The addBody will add one segment of the body to originalBody.py in each iteration. This is done by read and write function.



For example,



in the first iteration (initiated from main.py), originalBody.py will look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body


in second iteration, originalBody.py will automatically look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
object2 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body
bodySegment[1] = 2 #two body


When I'm trying to call the class from main.py,



main.py



from originalBody import BODY

for i in range (0,10):

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = BODY()
print(parent.bodySegment)


in the first iteration it runs smoothly:



{0: 1}


In the second iteration, after adding the body segment to originalBody.py, supposedly the result is expected to print out:



{0: 1, 1: 2}


However, it prints out the result from the first iteration:



{0: 1}


I suspect that my code calls the function from the original version of originalBody.py that has not modified yet. Do I need to add in some code in my main to get the class method from the modified version of originalBody? Or do I need to add in time delay to call the method right after the modification of originalBody?










share|improve this question














I'm new in python programming and would like to seek help in calling a method from another newly modified python file.



I currently have 3 python files: main.py, addBody.py, and originalBody.py.



The addBody will add one segment of the body to originalBody.py in each iteration. This is done by read and write function.



For example,



in the first iteration (initiated from main.py), originalBody.py will look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body


in second iteration, originalBody.py will automatically look like:



class BODY:
def __init__(self):
object1 = send_cylinder(length = 1.0, radius = 0.1)
object2 = send_cylinder(length = 1.0, radius = 0.1)
bodySegment = {}
bodySegment[0] = 1 #one body
bodySegment[1] = 2 #two body


When I'm trying to call the class from main.py,



main.py



from originalBody import BODY

for i in range (0,10):

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = BODY()
print(parent.bodySegment)


in the first iteration it runs smoothly:



{0: 1}


In the second iteration, after adding the body segment to originalBody.py, supposedly the result is expected to print out:



{0: 1, 1: 2}


However, it prints out the result from the first iteration:



{0: 1}


I suspect that my code calls the function from the original version of originalBody.py that has not modified yet. Do I need to add in some code in my main to get the class method from the modified version of originalBody? Or do I need to add in time delay to call the method right after the modification of originalBody?







python-3.x class methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:57









Nadiah NazNadiah Naz

162




162













  • Seems you may want to reload the file? Did you look into imp---particularly load_source?

    – mattsap
    Nov 21 '18 at 15:58











  • So you modify your already loaded code at runtime? Why?

    – MisterMiyagi
    Nov 21 '18 at 16:09











  • @mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

    – Nadiah Naz
    Nov 21 '18 at 16:37











  • @MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

    – Nadiah Naz
    Nov 21 '18 at 16:40











  • It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

    – MisterMiyagi
    Nov 21 '18 at 16:47



















  • Seems you may want to reload the file? Did you look into imp---particularly load_source?

    – mattsap
    Nov 21 '18 at 15:58











  • So you modify your already loaded code at runtime? Why?

    – MisterMiyagi
    Nov 21 '18 at 16:09











  • @mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

    – Nadiah Naz
    Nov 21 '18 at 16:37











  • @MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

    – Nadiah Naz
    Nov 21 '18 at 16:40











  • It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

    – MisterMiyagi
    Nov 21 '18 at 16:47

















Seems you may want to reload the file? Did you look into imp---particularly load_source?

– mattsap
Nov 21 '18 at 15:58





Seems you may want to reload the file? Did you look into imp---particularly load_source?

– mattsap
Nov 21 '18 at 15:58













So you modify your already loaded code at runtime? Why?

– MisterMiyagi
Nov 21 '18 at 16:09





So you modify your already loaded code at runtime? Why?

– MisterMiyagi
Nov 21 '18 at 16:09













@mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

– Nadiah Naz
Nov 21 '18 at 16:37





@mattsap should be considered as that since I want to directly get the modified file. I haven't look into that yet, will try to search for it

– Nadiah Naz
Nov 21 '18 at 16:37













@MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

– Nadiah Naz
Nov 21 '18 at 16:40





@MisterMiyagi I wanna let the system to modify the code by itself to automatically add body segment in each iteration

– Nadiah Naz
Nov 21 '18 at 16:40













It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

– MisterMiyagi
Nov 21 '18 at 16:47





It is unclear why that needs modifying the original class and especially the source code. Subclassing would allow representing distinct classes with different capabilities, and eval would allow reusability. Currently, every time you run the program more body parts are added...

– MisterMiyagi
Nov 21 '18 at 16:47












1 Answer
1






active

oldest

votes


















0














You can reload a module when it has already been imported by using the reload builtin function.



I edited your code using import originalBody because I am not sure how the reload function works with the from x import y syntax.



Code below:



from importlib import reload
import originalBody

for i in range (0,10):
reload(originalBody)

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = originalBody.BODY()
print(parent.bodySegment)





share|improve this answer
























  • Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

    – Nadiah Naz
    Nov 21 '18 at 16:49











  • Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

    – FMarazzi
    Nov 21 '18 at 17:04












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415914%2fpython-how-do-i-call-class-method-from-another-newly-modified-python-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can reload a module when it has already been imported by using the reload builtin function.



I edited your code using import originalBody because I am not sure how the reload function works with the from x import y syntax.



Code below:



from importlib import reload
import originalBody

for i in range (0,10):
reload(originalBody)

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = originalBody.BODY()
print(parent.bodySegment)





share|improve this answer
























  • Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

    – Nadiah Naz
    Nov 21 '18 at 16:49











  • Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

    – FMarazzi
    Nov 21 '18 at 17:04
















0














You can reload a module when it has already been imported by using the reload builtin function.



I edited your code using import originalBody because I am not sure how the reload function works with the from x import y syntax.



Code below:



from importlib import reload
import originalBody

for i in range (0,10):
reload(originalBody)

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = originalBody.BODY()
print(parent.bodySegment)





share|improve this answer
























  • Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

    – Nadiah Naz
    Nov 21 '18 at 16:49











  • Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

    – FMarazzi
    Nov 21 '18 at 17:04














0












0








0







You can reload a module when it has already been imported by using the reload builtin function.



I edited your code using import originalBody because I am not sure how the reload function works with the from x import y syntax.



Code below:



from importlib import reload
import originalBody

for i in range (0,10):
reload(originalBody)

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = originalBody.BODY()
print(parent.bodySegment)





share|improve this answer













You can reload a module when it has already been imported by using the reload builtin function.



I edited your code using import originalBody because I am not sure how the reload function works with the from x import y syntax.



Code below:



from importlib import reload
import originalBody

for i in range (0,10):
reload(originalBody)

fileToRead = open('addBody.py')
fileToAppend = open('originalBody.py', 'a')
...(add body from addBody.py to originalBody.py)...
fileToAppend.close()
fileToRead.close()

parent = originalBody.BODY()
print(parent.bodySegment)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:59









FMarazziFMarazzi

328213




328213













  • Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

    – Nadiah Naz
    Nov 21 '18 at 16:49











  • Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

    – FMarazzi
    Nov 21 '18 at 17:04



















  • Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

    – Nadiah Naz
    Nov 21 '18 at 16:49











  • Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

    – FMarazzi
    Nov 21 '18 at 17:04

















Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

– Nadiah Naz
Nov 21 '18 at 16:49





Thanks a lot! It helps to solve my issue. Just that reload() is for python 2. In my case for python 3.3, 'import imp imp.reload()' will do the trick

– Nadiah Naz
Nov 21 '18 at 16:49













Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

– FMarazzi
Nov 21 '18 at 17:04





Sorry, I should have specified the version. Anyway, reload exists since python 3.4 onwards!

– FMarazzi
Nov 21 '18 at 17:04




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415914%2fpython-how-do-i-call-class-method-from-another-newly-modified-python-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?