Re: Inheritance Question

Discussion in 'Python' started by Oscar Benjamin, Oct 18, 2012.

  1. On 18 October 2012 15:10, 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
    >
    > 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
    Oscar Benjamin, Oct 18, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,135
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    815
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,036
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,895
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    769
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page