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


Groups > comp.lang.python > #31670

Re: Inheritance Question

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'value,': 0.03; 'jeff': 0.04; 'attribute': 0.05; 'subject:Question': 0.07; 'python': 0.09; 'attribute.': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:b 70': 0.09; 'def': 0.10; 'language': 0.14; 'a()': 0.16; 'attributes,': 0.16; 'b()': 0.16; 'googling': 0.16; 'immutable,': 0.16; 'oct': 0.16; 'old-style': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subclasses.': 0.16; 'instance': 0.17; 'integer': 0.17; 'specify': 0.17; 'thu,': 0.17; 'define': 0.20; 'class.': 0.23; 'sets': 0.23; 'statement': 0.23; '(which': 0.26; 'creating': 0.26; 'common': 0.26; '(see': 0.27; '[1]': 0.27; 'skip:b 30': 0.27; '[2]': 0.27; 'header:X-Complaints-To:1': 0.28; 'all.': 0.28; 'trouble': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'classes': 0.30; 'print': 0.32; 'defining': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'thanks': 0.34; 'skip:a 70': 0.35; 'received:org': 0.36; 'but': 0.36; 'charset:us- ascii': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'skip:a 30': 0.60; 'different': 0.63; 'behavior': 0.64; 'potentially': 0.66; 'everybody': 0.69; 'dennis': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Inheritance Question
Date Thu, 18 Oct 2012 14:28:37 -0400
Organization > Bestiaria Support Staff <
References <CANNB2xtaLMC49Y=xKoJn_7jNEJixzUsQZa019wZZw+d64cf3rg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-16-250.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2459.1350584926.27098.python-list@python.org> (permalink)
Lines 125
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350584926 news.xs4all.nl 6885 [2001:888:2000:d::a6]:41352
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31670

Show key headers only | 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