I have the following class in python
class myTest:
def __init__(self, str):
self.str = str
def __unicode__(self):
return self.str
and in some other file a instantiate myTest to try out the unicode() method
import myClass
c = myClass.myTest("hello world")
print c
as print out I get <myClass.myTest instance at 0x0235C8A0> however if I overrider __str__() I will get hello world as output. My question is, how should I write the overrider for __unicode__() if I want it to output string instead?
Generally it is done like this:
This is because
__unicode__is not called implicitly in they way that__str__and__repr__are. It is called under the hood by the built-in functionunicode, so if you don’t define__str__you would have to do: