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


Groups > comp.lang.python > #31646 > unrolled thread

Re: Inheritance Question

Started byDave Angel <d@davea.name>
First post2012-10-18 10:51 -0400
Last post2012-10-18 10:51 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Inheritance Question Dave Angel <d@davea.name> - 2012-10-18 10:51 -0400

#31646 — Re: Inheritance Question

FromDave Angel <d@davea.name>
Date2012-10-18 10:51 -0400
SubjectRe: Inheritance Question
Message-ID<mailman.2441.1350571938.27098.python-list@python.org>
On 10/18/2012 10:10 AM, Jeff Jeffries wrote:
> Hello everybody
>
> When I set "AttributeChanges" in my example, it sets the same value for all
> other subclasses. Can someone help me with what the name of this behavior
> is (mutable class global?) ? .... I don't know any keywords... having
> trouble googling it
>

I can't understand your code or what you're trying to do with it, but
maybe i can help anyway.  Incidentally, putting code in an attachment
will hide it from many users of this mailing list.  Just paste it inline
in your message, and make sure your message is composed as text, not html.


Attributes can be attached to the class or to the instance.  Those
attached to the class are shared among all instances that don't hide
them by having instance attributes of the same name.

Any attribute bound in an instance method is specific to that instance. 
Attributes bound in the class itself belong to the class.

class MyClass:
    classAttr1 = 42           #this is a class attribute
    classAttr2 = "will be masked"   #also this
    def __init__(self):
        self.instance_attr = "each instance gets its own"
        self.classAttr2 = "this makes an instance attribute of the same
name"

    def test(self):
        print self.classAttr1    #prints 42
        print self.classAttr2   #prints    this makes an ...
        print MyClass.classAttr2  #prints    will be masked

a = MyClass()
a.test()




-- 

DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web