I’m trying to get the top node of a chain in getTopParent(). When I print out self.name, it indeed prints out the name of the parent instance; however, when I return self, it returns None. Why is this?
class A:
def __init__( self, name ):
self.name = name
self.owner = None
def setChild( self, l ):
l.owner = self
def getTopParent( self ):
if( self.owner == None ): # None == top parent
print( "Got top: %s" % self.name )
return self
else:
print( "checking %s" % self.name )
self.owner.getTopParent()
a = A( "parent" )
b = A( "child1" )
c = A( "child2" )
d = A( "child3" )
a.setChild( b )
b.setChild( c )
c.setChild( d )
print( d.getTopParent() )
>>> checking child3
checking child2
checking child1
Got top: parent
None
You need to return
self.owner.getTopParent()!In Python, you must actually use the
returnstatement to return something from a function, otherwiseNoneis returned.