Inheritance Question

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 18 10:31:36 EDT 2012


On 18 October 2012 15:10, Jeff Jeffries <jeff.jeffries.iii at gmail.com> 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
>
> Thanks in advance,

The code you attached is not so big that it can't just go in the
email. Here it is:

"""
class ABMixin:
    AttributeChanges = [1]
    AttributeDontChangeImmutable = 1
    def __init__(self):
        self.AttributeDontChange = [1]

class A(object,ABMixin):
    def __init__(self):
        ABMixin.__init__(self)

class B(object,ABMixin):
    def __init__(self):
        ABMixin.__init__(self)

a = A()
b = B()

print a.AttributeChanges,a.AttributeDontChange,a.AttributeDontChangeImmutable
print b.AttributeChanges,b.AttributeDontChange,b.AttributeDontChangeImmutable

a.AttributeChanges[0] = 2
a.AttributeDontChange[0] = 2
a.AttributeDontChangeImmutable = 2

print a.AttributeChanges,a.AttributeDontChange,a.AttributeDontChangeImmutable
print b.AttributeChanges,b.AttributeDontChange,b.AttributeDontChangeImmutable
"""

AttributeChanges is a "class attribute". This is the Python equivalent
of what would be a "static class member" in some other languages.


Oscar



More information about the Python-list mailing list