__str__() Doesn't Work After Importing Class
I encountered a problem while importing a class: the str() doesn't work after importing the class class unit: value = None node = None def __init__(self,value,node):
Solution 1:
Assuming you put both .py
files in the same directory, your Python 2.7
code runs fine with a standard CPython
interpreter such as Canopy
. Python 3.5
interpreters such as Anaconda will complain about the print
not being used as a function and will thus not execute.
Since your print
behaves as the default print
implementation for objects, you need to recompile (.pyc
files) the involved .py
files. This can easily be done by restarting your kernel in some IDE.
Canopy IDE allows you to run a .py
file again while the Python interpreter is still active. However, all objects created before this re-run stay the same and do not magically obtain your overridden __str__
member method.
Post a Comment for "__str__() Doesn't Work After Importing Class"