Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
class object's attribute is also the instance's attribute?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
陈伟  
View profile  
 More options Aug 30 2012, 6:55 am
Newsgroups: comp.lang.python
From: 陈伟 <chenwei.addr...@gmail.com>
Date: Thu, 30 Aug 2012 03:55:25 -0700 (PDT)
Local: Thurs, Aug 30 2012 6:55 am
Subject: class object's attribute is also the instance's attribute?
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Aug 30 2012, 7:54 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 30 Aug 2012 07:53:19 -0400
Local: Thurs, Aug 30 2012 7:53 am
Subject: Re: class object's attribute is also the instance's attribute?
On 08/30/2012 06:55 AM, 陈伟 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.

In your example, you have no instance attribute.  So when you use the
syntax to fetch one, the interpreter looks first at the instance,
doesn't find it, then looks in the class, and does.  That is documented
behavior.  Some people use it to provide a kind of default value for
instances, which can be useful if most instances need the same value,
but a few want to overrride it.

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ulrich Eckhardt  
View profile  
 More options Aug 30 2012, 7:22 am
Newsgroups: comp.lang.python
From: Ulrich Eckhardt <ulrich.eckha...@dominolaser.com>
Date: Thu, 30 Aug 2012 13:22:19 +0200
Local: Thurs, Aug 30 2012 7:22 am
Subject: Re: class object's attribute is also the instance's attribute?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Nawijn  
View profile  
 More options Aug 30 2012, 8:34 am
Newsgroups: comp.lang.python
From: Marco Nawijn <naw...@gmail.com>
Date: Thu, 30 Aug 2012 05:34:51 -0700 (PDT)
Local: Thurs, Aug 30 2012 8:34 am
Subject: Re: class object's attribute is also the instance's attribute?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Oscar Benjamin  
View profile  
 More options Aug 30 2012, 8:52 am
Newsgroups: comp.lang.python
From: Oscar Benjamin <oscar.j.benja...@gmail.com>
Date: Thu, 30 Aug 2012 13:52:43 +0100
Local: Thurs, Aug 30 2012 8:52 am
Subject: Re: class object's attribute is also the instance's attribute?
On Thu, 30 Aug 2012 05:34:51 -0700 (PDT), Marco Nawijn
<naw...@gmail.com> wrote:
> 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'

Except that in this case you'd need to do:
         self.d = 'my attribute'

Oscar


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
陈伟  
View profile  
 More options Aug 30 2012, 8:57 am
Newsgroups: comp.lang.python
From: 陈伟 <chenwei.addr...@gmail.com>
Date: Thu, 30 Aug 2012 05:57:32 -0700 (PDT)
Local: Thurs, Aug 30 2012 8:57 am
Subject: Re: class object's attribute is also the instance's attribute?
在 2012年8月30日星期四UTC+8下午7时54分35秒,Dave Angel写道:

thank you very much.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
陈伟  
View profile  
 More options Aug 30 2012, 8:57 am
Newsgroups: comp.lang.python
From: 陈伟 <chenwei.addr...@gmail.com>
Date: Thu, 30 Aug 2012 05:57:32 -0700 (PDT)
Local: Thurs, Aug 30 2012 8:57 am
Subject: Re: class object's attribute is also the instance's attribute?
在 2012年8月30日星期四UTC+8下午7时54分35秒,Dave Angel写道:

thank you very much.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Aug 30 2012, 9:25 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Thu, 30 Aug 2012 15:25:22 +0200
Local: Thurs, Aug 30 2012 9:25 am
Subject: Re: class object's attribute is also the instance's attribute?
On 30/08/12 14:34:51, Marco Nawijn wrote:

> Note that if you change 'd' it will change for all instances!

That depends on how you change it.

>>>> bobj = A()
>>>> bobj.d
> 'my attribute'

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

Here you change the attribute on the class.
That will affect all instances:

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

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

You can also set the attribute on an instance:

>>> bobj.d = 'For bobj only'
>>> bobj.d
'For bobj only'
>>>> aobj.d
> 'oops...attribute changed'

So, if you specifically change it on one instance, thenit won't
change on other instances of the same class.

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

That's a good idea, but it's not required.  You can set them
later, as shown above.

> class A(object):

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

That will just set the global variable d.
You want to set the instance attribute:

        self.d = 'my attribute'

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

>>>> aobj.d
> 'my attribute'

Note that aobj.d will not find the global variable d,
if neither the instance, nor the class nor any of the
base classes have that attribute.

I don't know where this 'my attribute' comes from, but
it's not the instance attribute you tried to set in the
__init__ method.  Maybe your class A still has a class
attribute with that value from an earlier experiment.

Hope this helps,

-- HansM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Nawijn  
View profile  
 More options Aug 30 2012, 10:11 am
Newsgroups: comp.lang.python
From: Marco Nawijn <naw...@gmail.com>
Date: Thu, 30 Aug 2012 07:11:06 -0700 (PDT)
Local: Thurs, Aug 30 2012 10:11 am
Subject: Re: class object's attribute is also the instance's attribute?

Learned my lesson today. Don't assume you know something. Test it first ;). I have done quite some programming in Python, but did not know that class attributes are still local to the instances. It is also a little surprising I must say. I always considered them like static variables in C++ (not that I am an expert in C++).

I knew of course that you don't have to define a local attribute in the __init__ method of a class, but I consider it good style and since the OP is a self claimed newbie I left out the other option.

The missing "self" in the code below was a typo
class A(object):

    def __init__(self):
        d = 'my attribute'   # should be self.d

Regards,

Marco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Aug 30 2012, 10:30 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 30 Aug 2012 10:30:35 -0400
Local: Thurs, Aug 30 2012 10:30 am
Subject: Re: class object's attribute is also the instance's attribute?
On 08/30/2012 10:11 AM, Marco Nawijn wrote:

> On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
>> <snip>

> Learned my lesson today. Don't assume you know something. Test it first ;). I have done quite some programming in Python, but did not know that class attributes are still local to the instances.

They're not.  They're just visible to the instances, except where the
instance has an instance attribute of the same name.  Don't be confused
by dir(), which shows both instance and class attributes.

Please show me an example where you think you observe each instance
getting a copy of the class attribute.  There's probably some other
explanation.

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Nawijn  
View profile  
 More options Aug 30 2012, 10:48 am
Newsgroups: comp.lang.python
From: Marco Nawijn <naw...@gmail.com>
Date: Thu, 30 Aug 2012 07:48:24 -0700 (PDT)
Local: Thurs, Aug 30 2012 10:48 am
Subject: Re: class object's attribute is also the instance's attribute?

I don't have an example. It was just what I thought would happen. Consider the following. In a class declaration like this:

class A(object):
    attr_1 = 10

    def __init__(self):
       self.attr_2 = 20

If I instantiated it twice:

obj_1 = A()
obj_2 = A()

For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after the following statement:

obj_1.attr_1 = 12

is that obj_2.attr_1 also equals 12. This is what surprised me a little, that's all.

Marco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Nawijn  
View profile  
 More options Aug 30 2012, 10:48 am
Newsgroups: comp.lang.python
From: Marco Nawijn <naw...@gmail.com>
Date: Thu, 30 Aug 2012 07:48:24 -0700 (PDT)
Local: Thurs, Aug 30 2012 10:48 am
Subject: Re: class object's attribute is also the instance's attribute?

I don't have an example. It was just what I thought would happen. Consider the following. In a class declaration like this:

class A(object):
    attr_1 = 10

    def __init__(self):
       self.attr_2 = 20

If I instantiated it twice:

obj_1 = A()
obj_2 = A()

For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after the following statement:

obj_1.attr_1 = 12

is that obj_2.attr_1 also equals 12. This is what surprised me a little, that's all.

Marco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Aug 30 2012, 11:18 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 30 Aug 2012 11:18:26 -0400
Local: Thurs, Aug 30 2012 11:18 am
Subject: Re: class object's attribute is also the instance's attribute?
On 08/30/2012 10:48 AM, Marco Nawijn wrote:

That statement only adds an instance attribute, not modifying the class
attribute of the same name.  But it does "hide" it from that particular
instance.

The thing that can be surprising is that if the class attribute is
mutable, and you mutate it, rather than assigning it.  So for example:

class A(object):
     attr_1 = [10, 9]

     def __init__(self):
        self.attr_2 = 20

 obj_1 = A()
 obj_2 = A()

obj_1.attr_1.append(3)

Then I believe you'll see [10, 9, 3] from both instances.
print obj_1.attr_1
print obj_2.attr_1

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Aug 30 2012, 11:21 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Thu, 30 Aug 2012 17:20:42 +0200
Local: Thurs, Aug 30 2012 11:20 am
Subject: Re: class object's attribute is also the instance's attribute?
On 30/08/12 16:48:24, Marco Nawijn wrote:

> On Thursday, August 30, 2012 4:30:59 PM UTC+2, Dave Angel wrote:
>> On 08/30/2012 10:11 AM, Marco Nawijn wrote:
>>> On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote:
>>>> <snip>
>>> Learned my lesson today. Don't assume you know something. Test it first ;).

A very important lesson.

Next week's lesson will be: if you test it first, then
paste it into a message for this forum, then tweak just
one unimportant detail, you'll need to test it again.

The trick is to look at obj_1.__dict__ to see what is defined locally:

>>> obj_1 = A()
>>> obj_1.__dict__

{'attr_2': 20}

>>> obj_1.attr_1 = 12
>>> obj_1.__dict__

{'attr_2': 20, 'attr_1': 12}

Hope this helps,

-- HansM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Aug 30 2012, 7:58 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Fri, 31 Aug 2012 09:58:49 +1000
Local: Thurs, Aug 30 2012 7:58 pm
Subject: Re: class object's attribute is also the instance's attribute?

Hans Mulder <han...@xs4all.nl> writes:
> Next week's lesson will be: if you test it first, then paste it into a
> message for this forum, then tweak just one unimportant detail, you'll
> need to test it again.

+1 QotW

--
 \        “Look at it this way: Think of how stupid the average person |
  `\         is, and then realise half of 'em are stupider than that.” |
_o__)                           —George Carlin, _Doin' It Again_, 1990 |
Ben Finney


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »