Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'assumed': 0.09; 'inherited': 0.09; 'kumar': 0.09; 'omit': 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; 'attribute.': 0.16; 'attributes.': 0.16; 'class)': 0.16; 'hierarchy': 0.16; 'instance"': 0.16; 'no-op': 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; 'examples': 0.20; 'saying': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'sorry,': 0.24; '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; '(2)': 0.35; 'there': 0.35; 'accessing': 0.36; 'method': 0.36; 'should': 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; 'show': 0.63; 'here': 0.66; 'otten': 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 09:04:26 +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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378969475 news.xs4all.nl 15946 [2001:888:2000:d::a6]:45006 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54039 Peter Otten wrote: > 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: Sorry, my examples don't show what I wanted to show. I assumed a class hierarchy confid <-- ExpectId <-- TestprintmsgId Also, ExpectId.__init__() need not invoke confid.__init__() in this particular case because both set the same attribute. The general ideas however still stand: (1) if you write an __init__() method it should invoke the __init__() method of the baseclass. (2) omit no-op __init__() methods altogether. Again, sorry for the confusion!