python - Why can't objects instantiated during __init__ see their creator? -
python - Why can't objects instantiated during __init__ see their creator? -
well i'm new so, oop , python too, please gentle ;)
i've looked threads , explainations related scoping issue elsewhere , haven't found any. grateful assistance.
sample code:
class zeus(object): def __init__(self): self.name='zeus' self.maze=maze() self.maze.get_zeus_name_1() self.maze.get_zeus_name_2(self) self.get_name_1() self.get_name_2(self) def get_name_1(self): try: print zeus.name except: print "impossible!?" def get_name_2(self,scope): print scope.name class maze(object): def get_zeus_name_1(self): try: print zeus.name except: print "can't done!?" def get_zeus_name_2(self,scope): print scope.name zeus=zeus() print 'now external calls:' zeus.maze.get_zeus_name_1() zeus.maze.get_zeus_name_2(zeus) zeus.get_name_1() zeus.get_name_2(zeus)
output:
can't done!? zeus impossible!? zeus external calls: zeus zeus zeus zeus
during instantiation of zeus
, if __init__
method creates instance of class, maze
, new instance not able access creator object, zeus
(unless self
passed it). (additionally, if __init__
method calls method within own class, get_name_1
, method cannot access objects attributes either (unless self
passed it).)
however, after objects both instantiated, sec object, maze
can recognise , access creator object, zeus
.
this behaviour has caused me confusion , difficulties, working on code initialised , run __init__
sequence - suppose improve avoid that..
my questions:
why case? are problems can arise through best avoided leaving instantiation, out of__init__
calls? are there farther design implications? passing self
new instance, seems though create problems, due self-referencing, should avoided? other interesting implications/ missing something? thanks help.
get_zeus_name_1()
trying access global variable zeus
, has not yet been initialised @ moment when get_zeus_name_1()
called.
get_zeus_name_2()
takes argument (scope
) , accesses that, works. doesn't seek access global variable zeus
.
same story get_name_1()
, get_name_2()
.
i think key point understand how python executes line:
zeus=zeus()
this line says python: execute method zeus()
(which name __init__(self)
method in zeus
class), , assign object returned method global variable zeus
.
python first executes method zeus()
, then, after init method has finished executing , returned object-instance of zeus class, python assigns object global variable zeus. global variable zeus has not been defined until after init method finishes, get_name_1() , get_zeus_name_1() cannot access it.
get_name_2() , get_zeus_name_2() access same object-instance of zeus class get_name_1() , get_zeus_name_1() seek access, access via parameter passed them, not via global variable, don't run problem.
here simpler illustration demonstrates same problem:
>>> class foo: ... def __init__(self): ... self.name = 'foobar' ... print self.name ... print foo.name ... >>> foo = foo() foobar traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "<stdin>", line 5, in __init__ nameerror: global name 'foo' not defined >>>
the print self.name
line works fine (equivalent get_name_2() , get_zeus_name_2()), print foo.name
line crashes (equivalent get_name_2() , get_zeus_name_2()).
python oop instantiation init
Comments
Post a Comment