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 attribute or instance 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
  5 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
 
Paul Watson  
View profile  
 More options Apr 28 2003, 2:45 pm
Newsgroups: comp.lang.python
From: "Paul Watson" <pwat...@redlinec.com>
Date: Mon, 28 Apr 2003 13:47:28 -0500
Local: Mon, Apr 28 2003 2:47 pm
Subject: Class attribute or instance attribute?
When is an attribute a class attribute or an instance attribute?

In the following code, is the attribute (variable/member) a class attribute
or an instance attribute?  I thought that it was an instance attribute and
that the class attribute was created in __init__().

My biggest question is why self.v has a value of "Consequences" in the
_init__() of f2?  Is self.v not an instance variable?

pwatson [ /home/pwatson/src/python/c ] 270
$ cat d.py
#! /usr/bin/env python

class whatisit:
  v = "Ambiguous"

  def __init__(self):
    print '=== __init__', self.v, whatisit.v
    self.v = "Truth"
    whatisit.v = "Consequences"

  def showem(self):
    print '=== showem  ', self.v, whatisit.v

if (__name__ == "__main__"):
  f1 = whatisit()
  f1.showem()
  f2 = whatisit()
  f2.showem()
pwatson [ /home/pwatson/src/python/c ] 271
$ ./d.py
=== __init__ Ambiguous Ambiguous
=== showem   Truth Consequences
=== __init__ Consequences Consequences
=== showem   Truth Consequences


 
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.
Michele Simionato  
View profile  
 More options Apr 28 2003, 5:29 pm
Newsgroups: comp.lang.python
From: m...@pitt.edu (Michele Simionato)
Date: 28 Apr 2003 14:29:33 -0700
Local: Mon, Apr 28 2003 5:29 pm
Subject: Re: Class attribute or instance attribute?

"Paul Watson" <pwat...@redlinec.com> wrote in message <news:vaqtlsqbi52j93@corp.supernews.com>...
> When is an attribute a class attribute or an instance attribute?

I give you a subtle example:

class C(object):
      x=1 # class attribute
      def __init__(self):
          self.x=self.x # the class attribute becomes an instance attribute

c=C()

C.x=2

print c.x,C.x

This code prints 1,2. You see that the line self.x=self.x is far
from being trivial: Python looks first for the self on the right,
looking for the instance attribute self.x: since it does not find anything,
it looks for the class attribute; then the class attribute
is assigned to the instance attribute on the left hand side.

If you forget the apparently useless self.x=self.x line, you get a
different result:

class C(object):
      x=1 # class attribute
      def __init__(self):
          pass

c=C()

C.x=2

print c.x,C.x

This prints 2,2, since the class attribute has been changed to 2
and c has no instance attribute.

Hoping-having-not-increased-your-confusion-ly,

                    Michele


 
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.
Aahz  
View profile  
 More options Apr 28 2003, 6:34 pm
Newsgroups: comp.lang.python
From: a...@pythoncraft.com (Aahz)
Date: 28 Apr 2003 16:39:00 -0400
Subject: Re: Class attribute or instance attribute?
In article <vaqtlsqbi52...@corp.supernews.com>,

Paul Watson <pwat...@redlinec.com> wrote:

>My biggest question is why self.v has a value of "Consequences" in the
>_init__() of f2?  Is self.v not an instance variable?

When an attribute lookup fails in an instance, Python searches the
namespace of the parent class and all of the parent's base classes (but
*not* the namespaces of the metaclasses).
--
Aahz (a...@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93


 
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.
Paul Watson  
View profile  
 More options Apr 28 2003, 11:34 pm
Newsgroups: comp.lang.python
From: "Paul Watson" <pwat...@redlinec.com>
Date: Mon, 28 Apr 2003 22:36:44 -0500
Local: Mon, Apr 28 2003 11:36 pm
Subject: Re: Class attribute or instance attribute?
Thank you for your reply.  I have finally gotten it that the attribute 'x'
is already a class attribute and not an instance attribute.  It appears that
there can only be an instance attribute when someone says self.x or
instance.x.  I used dir(C) and dir(self) to see what attributes existed.  Is
there any other way to declare instance variables?

Thanks for everyone's help, including Mike.

"Michele Simionato" <m...@pitt.edu> wrote in message

news:2259b0e2.0304281329.12ac184b@posting.google.com...
> "Paul Watson" <pwat...@redlinec.com> wrote in message

<news:vaqtlsqbi52j93@corp.supernews.com>...


 
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.
Asun Friere  
View profile  
 More options Apr 29 2003, 12:50 am
Newsgroups: comp.lang.python
From: afri...@yahoo.co.uk (Asun Friere)
Date: 28 Apr 2003 21:50:41 -0700
Local: Tues, Apr 29 2003 12:50 am
Subject: Re: Class attribute or instance attribute?
m...@pitt.edu (Michele Simionato) wrote in message <news:2259b0e2.0304281329.12ac184b@posting.google.com>...
> "Paul Watson" <pwat...@redlinec.com> wrote in message <news:vaqtlsqbi52j93@corp.supernews.com>...
> > When is an attribute a class attribute or an instance attribute?

> I give you a subtle example:

On the other hand here is a totally obvious example illustrating the same ;-)

class Foo (object) :
    foo = 'spam'
    def __init__ (self) :
        print Foo.foo, self.foo
        self.foo = 'ham'
        print Foo.foo, self.foo

>>> f = Foo()

spam spam
spam ham
>>> print f.foo, Foo.foo

ham spam

 
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 »