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


Groups > comp.lang.python > #31644

Re: Inheritance Question

References <CANNB2xtaLMC49Y=xKoJn_7jNEJixzUsQZa019wZZw+d64cf3rg@mail.gmail.com>
Date 2012-10-18 15:31 +0100
Subject Re: Inheritance Question
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2439.1350570697.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 18 October 2012 15:10, Jeff Jeffries <jeff.jeffries.iii@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Inheritance Question Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-10-18 15:31 +0100

csiph-web