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


Groups > comp.lang.python > #97827

Re: variable scope of class objects

Subject Re: variable scope of class objects
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <n04m96$tvd$1@speranza.aioe.org>
From Nagy László Zsolt <gandalf@shopzeus.com>
Date 2015-10-20 08:38 +0200
Newsgroups comp.lang.python
Message-ID <mailman.48.1445323088.878.python-list@python.org> (permalink)

Show all headers | View raw


> These two statements make me think you come from C++ or something
> similar.
>
> In Python you can declare variables at class level, but this
> declaration must NOT be interpreted in the same manner of a similar
> declaration in C++: they remain at the abstract level of a class, and
> they have nothing to do with an instance of a class (in fact, to be
> correctly invoked, they must be preceeded by the class name).
When you say "they have nothing to do", it is almost true but not 100%.

When accessing attributes of an instance, Python first searches in the
namespace of the instance. When not found, it searches in the namespace
of its class.

So for example:

>>>
>>> class A(object):
...     a = ["value 1"]
...     def set_a(self):
...         # This will bind the value  to the name "a" in the namespace
of the instance (!!!), not the class
...         self.a = ["value 3"]
...
>>> a = A()
>>> b = A()
>>> print A.a # ["value 1"]
['value 1']
>>> print a.a  # ["value 1"]
['value 1']
>>> print b.a  # ["value 1"]
['value 1']
>>> A.a.append("value 2")
>>> print A.a # ["value 1","value 2"]
['value 1', 'value 2']
>>> print a.a  # ["value 1","value 2"]
['value 1', 'value 2']
>>> print b.a  # ["value 1","value 2"]
['value 1', 'value 2']
>>> a.set_a()
>>> print a.a  # ["value 3"]
['value 3']
>>> print A.a # ["value 1","value 2"]
['value 1', 'value 2']
>>> print b.a # ["value 1","value 2"]
['value 1', 'value 2']
>>> print A.a is b.a # True
True
>>> print a.a is b.a # False
False
>>> b.a.append("value 4")
>>> A.a
['value 1', 'value 2', 'value 4']
>>> del a.a
>>> a.a
['value 1', 'value 2', 'value 4']
>>>

Some objects are designed this way: the attribute with the same name can
be bound to an object stored at class level or at instance level,
depending on how the object was created or used. In other words: when
you access an attrbute through an object, you can very well reach a
class attribute instead of an object attribute; and this behaviour can
be different for different instances of the same class. Just look at the
last attribute deletion - by deleting an attribute of an instance,
further attribute access will hit the class level object. The same
behaviour is unthinkable with C++.



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


Thread

variable scope of class objects JonRob - 2015-10-19 14:39 -0400
  Re: variable scope of class objects Random832 <random832@fastmail.com> - 2015-10-19 15:01 -0400
    Re: variable scope of class objects JonRob - 2015-10-20 17:11 -0400
  Re: variable scope of class objects sohcahtoa82@gmail.com - 2015-10-19 16:19 -0700
    Re: variable scope of class objects Terry Reedy <tjreedy@udel.edu> - 2015-10-19 20:03 -0400
  Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 07:31 +0200
  Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 08:17 +0200
    Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 08:38 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 09:23 +0200
    Re: variable scope of class objects JonRob - 2015-10-20 17:33 -0400
      Re: variable scope of class objects Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-10-20 20:18 -0400
        Re: variable scope of class objects JonRob - 2015-10-21 19:35 -0400
          Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 11:59 +0200
      What does it mean for Python to have “constants”? (was: variable scope of class objects) Ben Finney <ben+python@benfinney.id.au> - 2015-10-21 11:27 +1100
      Re: What does it mean for Python to have “constants”? Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-21 08:13 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 07:55 +0200
      Re: variable scope of class objects Erik <python@lucidity.plus.com> - 2015-10-20 23:17 +0100

csiph-web