Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13312

Re: method:wrong structure

References <tencent_7D324E33209E90851683965E@qq.com>
Date 2011-09-15 01:39 -0700
Subject Re: method:wrong structure
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1155.1316075991.27778.python-list@python.org> (permalink)

Show all headers | View raw


2011/9/15 守株待兔 <1248283536@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: method:wrong structure Chris Rebert <clp2@rebertia.com> - 2011-09-15 01:39 -0700

csiph-web