Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97202
| Path | csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!ecngs!feeder2.ecngs.de!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Question': 0.05; 'assignment': 0.07; 'builtin': 0.07; '@property': 0.09; 'assigning': 0.09; 'attribute.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'def': 0.13; 'attribute"': 0.16; 'attribute,': 0.16; 'benefit.': 0.16; 'code),': 0.16; 'instance:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'ways:': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'assuming': 0.22; 'variables.': 0.22; 'cheers,': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'function': 0.28; 'idea': 0.28; 'values': 0.28; 'fine': 0.28; "i'm": 0.30; 'print': 0.30; "we're": 0.30; 'creating': 0.30; 'code': 0.30; "i'd": 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'problem': 0.33; 'instance': 0.35; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'starting': 0.37; 'things': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'received:194': 0.61; 'side': 0.62; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'making': 0.62; '(that': 0.63; 'finally': 0.70; 'yourself': 0.73; 'topic,': 0.79; 'confusing': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | jmp <jeanmichel@sequans.com> |
| Subject | Re: Question re class variable |
| Date | Tue, 29 Sep 2015 13:02:12 +0200 |
| References | <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | paris.sequans.com |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
| In-Reply-To | <3948d9cd-24b1-4a3b-8ed0-46bb60a8d738@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.233.1443524549.28679.python-list@python.org> (permalink) |
| Lines | 58 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1443524549 news.xs4all.nl 23770 [2001:888:2000:d::a6]:37705 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:97202 |
Show key headers only | View raw
On 09/29/2015 11:27 AM, plewto@gmail.com wrote:
> I have a perplexing problem with Python 3 class variables.
Your problem is that when assigning values to your class attribute, you
are actually creating a instance attribute.
class Foo:
bar = "I'm a class attribute"
def __init__(self):
self.bar = "I'm an instance attribute"
def foo(self):
print self.bar
print Foo.bar
# this is how you set a class attribute from an instance
Foo.bar = "I am still a class attribute"
print Foo.bar
Foo.foo()
I'm an instance attribute
I'm a class attribute
I am still a class attribute
What can be confusing is that assuming you never use the same name for a
class an instance attribute (that would be bad code), you can access
your class attribute from the instance:
class Foo:
bar = "I'm a class attribute"
def foo(self):
# python will look into the class scope if not found in the instance
print self.bar # this is not an assignment so we're fine
Foo.foo()
I'm an class attribute
As side note and unrelated topic, your are using name mangling
(attribute starting with __), are you sure you need it ? You need a
strong motive to use this feature otherwise you're making things
difficult for yourself without any benefit.
Finally here's how I'd code your id, to give some idea on alternative ways:
class GameObject:
@property
def id(self):
return id(self) #use the builtin id function
print GameObject().id
Cheers,
JM
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question re class variable plewto@gmail.com - 2015-09-29 02:27 -0700
Re: Question re class variable alister <alister.nospam.ware@ntlworld.com> - 2015-09-29 10:00 +0000
Re: Question re class variable John Gordon <gordon@panix.com> - 2015-09-29 14:44 +0000
Re: Question re class variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-29 20:41 -0400
Re: Question re class variable Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-09-29 12:40 +0200
Re: Question re class variable Anssi Saari <as@sci.fi> - 2015-09-29 14:17 +0300
Re: Question re class variable Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-09-29 14:02 +0200
Re: Question re class variable Steven D'Aprano <steve@pearwood.info> - 2015-09-29 22:06 +1000
Re: Question re class variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-29 08:21 -0400
Re: Question re class variable jmp <jeanmichel@sequans.com> - 2015-09-29 13:02 +0200
Re: Question re class variable jmp <jeanmichel@sequans.com> - 2015-09-29 13:11 +0200
csiph-web