method:wrong structure
Chris Rebert
clp2 at rebertia.com
Thu Sep 15 04:39:48 EDT 2011
2011/9/15 守株待兔 <1248283536 at qq.com>:
> there are three programs,all of them left main structure,
> code0 is ok,i don't know why code2 and code3 can't run,how to fix them?
> code0
> class webdata(object):
> def __init__(self,arg):
>
> def loadequity(self):
>
> def loadoption(self):
>
> #loadstatus={'equity':self.loadequity(),'option':self,loadoption()}
>
> def run(self):
>
> if __name__=="__main__":
> s=webdata('equity').run()
> s.loadequity()
>
> code1
> class webdata(object):
> def __init__(self,arg):
>
> def loadequity(self):
>
> def loadoption(self):
>
> loadstatus={'equity':self.loadequity(),'option':self,loadoption()}
Class definitions are executable code. So the assignment to loadstatus
happens *while the class webdata is still being defined*. There is
therefore no class instance object or `self` at this point, hence the
error when you try and do self.loadequity(): you're trying to call an
instance method before it's even possible for there to be instances of
the class.
Cheers,
Chris
--
http://rebertia.com
More information about the Python-list
mailing list