I've coded a parent class and a nested class. I've already overridden the parent class __str__
method and now I want to do the same on the nested class. But for some reason the __str__
method in the nested class is not getting called. And I can't see the instance variables in PyDev's autocompletion list when coding inside the nested class's str. Code:
class Parent(object):
def __init__(self):
self.parent_var = "Parent"
def __str__(self):
return self.parent_var
class Nested(object):
def __init__(self):
#Works (of course, why shouldn't it?)
self.children_var = "Child"
def __str__(self):
# This method never called!
return self.children_var
Something odd is going on. Why can't the __str__
method just work like the __init__
did?
UPDATE:
You guys are right. The code was ok, but my str was trying to do this:
return "Some string" + some_integer
And apparently in Python you need to do str(some_integer) before concatenating stuff (this is actually lame).
答案 0 :(得分:1)
print Parent.Nested() # works fine, print calls __str__ resulting in 'Child'