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


Groups > comp.lang.python > #28109

Re: class object's attribute is also the instance's attribute?

Newsgroups comp.lang.python
Date 2012-08-30 05:34 -0700
References <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com>
Message-ID <e8b14404-2340-4743-b66b-e9afb701e676@googlegroups.com> (permalink)
Subject Re: class object's attribute is also the instance's attribute?
From Marco Nawijn <nawijn@gmail.com>

Show all headers | View raw


On Thursday, August 30, 2012 12:55:25 PM UTC+2, 陈伟 wrote:
> when i write code like this:
> 
> 
> 
> class A(object):
> 
>      
> 
>     d = 'it is a doc.'
> 
>     
> 
> 
> 
> t = A()
> 
> 
> 
> print t.__class__.d
> 
> print t.d
> 
> 
> 
> the output is same.
> 
> 
> 
> so it means class object's attribute is also the instance's attribute. is it right? i can not understand it.

I think the best way is to think of it as a global attribute restricted to the class A. Note that you even don't need an instance to get the value of the attribute. You can directly get it from the class itself.

>>> class A(object): d = 'my attribute'
>>> A.d
'my attribute'
>>> aobj = A()
>>> aobj.d
'my attribute'

Note that if you change 'd' it will change for all instances!
>>> bobj = A()
>>> bobj.d
'my attribute'

>>> A.d = 'oops...attribute changed'
>>> aobj.d
'oops...attribute changed'

>>> bobj.d
'oops...attribute changed'

If you want attributes to be local to the instance, you have to define them in the __init__ section of the class like this:

class A(object):

   def __init__(self):
        d = 'my attribute'

>>> aobj = A()
>>> bobj = A()

>>> aobj.d
'my attribute'

>>> bobj.d
'my attribute'

>>> aobj.d = 'oops...attribute changed'

>>> aobj.d
'oops...attribute changed'

>>> bobj.d
'my attribute'

Regards,

Marco

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


Thread

class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 03:55 -0700
  Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 07:53 -0400
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
  Re: class object's attribute is also the instance's attribute? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 13:22 +0200
  Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 05:34 -0700
    Re: class object's attribute is also the instance's attribute? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-08-30 13:52 +0100
    Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 15:25 +0200
      Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:11 -0700
        Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 10:30 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 11:18 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 17:20 +0200
              Re: class object's attribute is also the instance's attribute? Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 09:58 +1000

csiph-web