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


Groups > comp.lang.python > #31670

Re: Inheritance Question

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Inheritance Question
Date 2012-10-18 14:28 -0400
Organization > Bestiaria Support Staff <
References <CANNB2xtaLMC49Y=xKoJn_7jNEJixzUsQZa019wZZw+d64cf3rg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2459.1350584926.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, 18 Oct 2012 10:10:23 -0400, Jeff Jeffries
<jeff.jeffries.iii@gmail.com> declaimed the following in
gmane.comp.python.general:

> 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,

> """python 2.7"""
> 
> class ABMixin:

	Since you specify Python 2.7, this is defining an OLD-style class. I
don't know how well using old-style classes work when subclassed against
a new style class (which the following two are, using "object".

	class ABMixin(object):


>     AttributeChanges = [1]
>     AttributeDontChangeImmutable = 1

	These two define classwide attributes, common to all.

>     def __init__(self):
>         self.AttributeDontChange = [1]
>     
> class A(object,ABMixin):

	You don't need "object" on these if you derive ABMixin from "object"
(see section 3.3 of the language reference manual)

	
>     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

	In Python, it is the VALUE that is mutable or immutable, not the
NAME. Nothing you do in Python will change an integer "1" into an
integer "2"... But the name that started off attached to the 1 CAN be
reattached to the 2

	This statement is potentially not only rebinding the name to a
different value, but in this case is actually creating a NEW instance
attribute which shadows the class-wide attribute.


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

-=-=-=-=-
"""python 2.7"""

class ABMixin(object):
    AttributeChanges = [1]
    AttributeDontChangeImmutable = 1
    def __init__(self):
        self.AttributeDontChange = [1]
    
class A(ABMixin):
    def __init__(self):
        ABMixin.__init__(self)
        
class B(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,
A.AttributeDontChangeImmutable
print
b.AttributeChanges,b.AttributeDontChange,b.AttributeDontChangeImmutable,
B.AttributeDontChangeImmutable

print id(1), id(2)
print id(a.AttributeDontChangeImmutable),
id(A.AttributeDontChangeImmutable)
print id(b.AttributeDontChangeImmutable),
id(B.AttributeDontChangeImmutable)

>pythonw -u "demo.py"
[1] [1] 1
[1] [1] 1
[2] [2] 2 1
[2] [1] 1 1
10023248 10023236
10023236 10023248
10023248 10023248
>Exit code: 0

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


Thread

Re: Inheritance Question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-18 14:28 -0400

csiph-web