Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'attribute': 0.07; 'class,': 0.07; 'inherited': 0.09; 'kumar': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'val': 0.09; 'python': 0.11; 'def': 0.12; '"created': 0.16; 'attributes.': 0.16; 'class)': 0.16; 'instance"': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Accessing': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'saying': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'invoke': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'classes': 0.35; 'there': 0.35; 'accessing': 0.36; 'method': 0.36; 'wrong': 0.37; 'to:addr:python-list': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'new': 0.61; 'complete': 0.62; 'here': 0.66; 'object:': 0.84; ',please': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Accessing class attribute Date: Thu, 12 Sep 2013 08:45:21 +0200 Organization: None References: <1378966547.35546.YahooMailBasic@web190504.mail.sg3.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508481fe.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378968339 news.xs4all.nl 15872 [2001:888:2000:d::a6]:40263 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54038 chandan kumar wrote: > Hi , > > I'm new to python ,please correct me if there is any thing wrong with the > way accessing class attributes. > > Please see the below code .I have inherited confid in ExpectId class, > changed self.print_msg to Hello. Now inherited confid in TestprintmsgID > class.Now I wanted to print self.print_msg value (which is changed under > ExpectId class) as Hello under TestprintmsgID. I end up with error saying > TestprintmsgID has no attribute self.print_msg. Atleast i expect the null > to be printed. > > > > class confid(): > def __init__(self): > self.print_msg = "" > > class ExpectId(confid): > > def __init__(self): > self.print_msg = " Hello" > > def expectmethod(self): > print "self.print_mesg = ",self.print_msg > > class TestprintmsgID(confid): > > def __init__(self): > "Created Instance" > > def printmsgmethod(self): > print "printmsgmethod print_msg val = ",self.print_msg---- Here is > the Attribute error If the class has an __init__() method the initializer of the base class is not invoked automatically. In the above code the initializer of TestprintmsgId does nothing, so you can avoid it. ExpectId.__init__() however must invoke confid.__init_(). The complete example: class confid(): def __init__(self): self.print_msg = "" class ExpectId(confid): def __init__(self): confid.__init__(self) self.print_msg = " Hello" def expectmethod(self): print "self.print_mesg = ",self.print_msg class TestprintmsgID(confid): def printmsgmethod(self): print "printmsgmethod print_msg val = ",self.print_msg Note that there is an alternative way to invoke a baseclass method using super() which works only for "newstyle classes", i. e. classes that inherit from object: class confid(object): def __init__(self): self.print_msg = "" class ExpectId(confid): def __init__(self): super(ExpectId, self).__init__() self.print_msg = " Hello" def expectmethod(self): print "self.print_mesg = ",self.print_msg class TestprintmsgID(confid): def printmsgmethod(self): print "printmsgmethod print_msg val = ",self.print_msg