Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'instance': 0.05; 'definitions': 0.07; 'skip:l 60': 0.09; 'subject:method': 0.09; 'run(self):': 0.16; 'skip:# 60': 0.16; 'subject:wrong': 0.16; 'cc:addr:python-list': 0.16; 'cheers,': 0.18; 'trying': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'code.': 0.26; 'skip:_ 20': 0.28; 'message-id:@mail.gmail.com': 0.29; 'fix': 0.29; 'cc:addr:python.org': 0.30; '\xc2\xa0\xc2\xa0\xc2\xa0': 0.30; 'class': 0.30; 'error': 0.32; 'chris': 0.32; 'point,': 0.32; "can't": 0.33; 'there': 0.33; 'assignment': 0.34; 'executable': 0.34; 'object': 0.35; 'class.': 0.37; 'received:google.com': 0.38; 'subject:: ': 0.39; 'why': 0.39; 'received:74.125': 0.39; "it's": 0.40; 'happens': 0.40; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'subject::': 0.87; 'to:none': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type :content-transfer-encoding; bh=n5gTRDsg2t2ne/f/O5DNfspmV14knhvE2mGbGds/hmM=; b=SjQCLGr8Bm57d9j+t+FUkuRyFhfVMDzb1Ojq4j3d60OTu7yvSN2lOdroxDvRS9JpbX nmQOgIWOkKAr0n2xIjkoCkk9m4NLZ0IIh6QZ9FNfvEtjjuaxDq9fOvb7XI9CAdQ/BEfe GYAbOqNSlpdHaptVCEQ6nfWD0HqdZ9lJhJZJM= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Thu, 15 Sep 2011 01:39:48 -0700 X-Google-Sender-Auth: z9gAkYwQjlLXAVxKhJdLalcDAHY Subject: Re: method:wrong structure From: Chris Rebert Cc: python-list Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1316075991 news.xs4all.nl 2451 [2001:888:2000:d::a6]:43150 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13312 2011/9/15 =E5=AE=88=E6=A0=AA=E5=BE=85=E5=85=94 <1248283536@qq.com>: > there are=C2=A0 three=C2=A0 programs,all of them=C2=A0 left=C2=A0 main=C2= =A0 structure, > code0 is ok,i don't know why code2 and code3 can't run,how to fix them? > code0 > class=C2=A0=C2=A0 webdata(object): > =C2=A0=C2=A0=C2=A0 def=C2=A0 __init__(self,arg): > > =C2=A0=C2=A0=C2=A0 def=C2=A0 loadequity(self): > > =C2=A0=C2=A0=C2=A0 def=C2=A0 loadoption(self): > > =C2=A0=C2=A0=C2=A0 #loadstatus=3D{'equity':self.loadequity(),'option':sel= f,loadoption()} > > =C2=A0=C2=A0=C2=A0 def=C2=A0 run(self): > > if=C2=A0 __name__=3D=3D"__main__": > =C2=A0=C2=A0=C2=A0=C2=A0 s=3Dwebdata('equity').run() > =C2=A0=C2=A0=C2=A0=C2=A0 s.loadequity() > > code1 > class=C2=A0=C2=A0 webdata(object): > =C2=A0=C2=A0=C2=A0 def=C2=A0 __init__(self,arg): > > =C2=A0=C2=A0=C2=A0 def=C2=A0 loadequity(self): > > =C2=A0=C2=A0=C2=A0 def=C2=A0 loadoption(self): > > =C2=A0=C2=A0=C2=A0 loadstatus=3D{'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