Inheritance Question

Dave Angel d at davea.name
Thu Oct 18 10:51:55 EDT 2012


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




More information about the Python-list mailing list