Python - How do I call class method from another newly modified python file
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
|
show 1 more comment
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
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, andeval
would allow reusability. Currently, every time you run the program more body parts are added...
– MisterMiyagi
Nov 21 '18 at 16:47
|
show 1 more comment
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
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
python-3.x class methods
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, andeval
would allow reusability. Currently, every time you run the program more body parts are added...
– MisterMiyagi
Nov 21 '18 at 16:47
|
show 1 more comment
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, andeval
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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