Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Inheritance Question

14 views
Skip to first unread message

Oscar Benjamin

unread,
Oct 18, 2012, 10:31:36 AM10/18/12
to Jeff Jeffries, pytho...@python.org
On 18 October 2012 15:10, Jeff Jeffries <jeff.jef...@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

Dave Angel

unread,
Oct 18, 2012, 10:51:55 AM10/18/12
to Jeff Jeffries, pytho...@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

Message has been deleted
0 new messages