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


Groups > comp.lang.python > #28104

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

From Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Newsgroups comp.lang.python
Subject Re: class object's attribute is also the instance's attribute?
Date 2012-08-30 13:22 +0200
Message-ID <503F4CEB.9070408@dominolaser.com> (permalink)
References <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com>

Show all headers | View raw


Am 30.08.2012 12:55, schrieb 陈伟:
> class A(object):
>      d = 'it is a doc.'
>
> t = A()
>
> print t.__class__.d
> print t.d
>
> the output is same.

You could go even further:

print id(t.__class__.d)
print id(t.d)

which should show you that they are not just equal but identical.


> so it means class object's attribute is also the instance's
> attribute.is it right?

Yes. This is even useful sometimes:

class Point(object):
     x = 0
     y = 0

This will cause every Point to have two attributes x and y that have a 
default value 0.


Note that setting this attribute on an instance does not change the 
class' attribute, just in that that was what confused you. However, if 
the attribute references a mutable type (e.g. a list) this can cause 
problems because the instance (see id() above) is the same and thus 
modifications affect both the class and all instances.


Uli

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