Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Class level variables in Python

Reply
Thread Tools

Class level variables in Python

 
 
Brian Munroe
Guest
Posts: n/a
 
      08-27-2003
I am just starting to learn the OO side of Python scripting, and I am
a little confused on the following. Take the following example class:

>>> class rectangle(object):

z = 1
def __init__(self):
self.x = 2

>>> r = rectangle()
>>> print r.z

1
>>> print r.x

2
>>> r.z = 16
>>> print r.z

16
>>> r.x = 17
>>> print r.x

17
>>>


I was wondering if someone could explain if there is any difference
between initalizing your object attributes up in the __init__
constructor or setting them up as (I am guessing at the name here)
object level variables (like z)

thanks

-- brian
 
Reply With Quote
 
 
 
 
mackstann
Guest
Posts: n/a
 
      08-27-2003
On Wed, Aug 27, 2003 at 04:43:03PM -0700, Brian Munroe wrote:
> I was wondering if someone could explain if there is any difference
> between initalizing your object attributes up in the __init__
> constructor or setting them up as (I am guessing at the name here)
> object level variables (like z)


Generally, if something is more or less constant, I make it a class
variable. If its value depends on the arguments passed to __init__, or
if it is something like a network connection, file operation, etc, then
it goes in __init__.

class Foo:
specialSequence = "blahblah"

def __init__(self, filename):
self.fp = file(filename, "r")
if self.fp.read( == self.specialSequence:
# .. do something ..

specialSequence has no reason to be in the constructor, but fp does. I
suppose that's a good guideline - if you can't think of a reason for it
to be in __init__, then don't put it there.

--
m a c k s t a n n mack @ incise.org http://incise.org
The four building blocks of the universe are fire, water, gravel and vinyl.
-- Dave Barry

 
Reply With Quote
 
 
 
 
Sean Ross
Guest
Posts: n/a
 
      08-28-2003
"Brian Munroe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I was wondering if someone could explain if there is any difference
> between initalizing your object attributes up in the __init__
> constructor or setting them up as (I am guessing at the name here)
> object level variables (like z)


Hi.
Yes there is a difference. One is an instance attribute, the other is a
class attribute:

>>> class C:

.... attr = 1
.... def __init__(self):
.... self.attr = 2
....
>>> c = C()
>>> print c.attr

2
>>> print c.__class__.attr

1
>>>


HTH
Sean


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      08-28-2003

"Brian Munroe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I am just starting to learn the OO side of Python scripting, and I

am
> a little confused on the following. Take the following example

class:
>
> >>> class rectangle(object):

> z = 1
> def __init__(self):
> self.x = 2
>
> >>> r = rectangle()
> >>> print r.z

> 1
> >>> print r.x

> 2
> >>> r.z = 16
> >>> print r.z

> 16
> >>> r.x = 17
> >>> print r.x

> 17
> >>>

>
> I was wondering if someone could explain if there is any difference
> between initalizing your object attributes up in the __init__
> constructor or setting them up as (I am guessing at the name here)
> object level variables (like z)


Everything set at 'top' level under the class statement is a class
attribute. Ditto for anything set outside the class statement as
someclass.attribute. This include instance methods, which are common
to all instances and therefore *attributes* of the class.

Everything set within instance methods as self.attribute or outside as
someinstance.attribute are instance attributes private to that
instance. Just as a function can have a private local variable with
the same name as a 'public' global variable, an instance can have an
attribute of the same name as an attribute of its class. Just as
function locals 'mask' the global of the same name, instance 'locals'
usually* mask the class attribute of the same name.

In your example above, you start with class attribute z and later add
an r instance attribute of same name (but different value). First you
see one, then the other.

(* I believe the masking exception alluded to above has something to
do with special methods, descriptors, and classes derived from
builtins, but I do not know the current rule will enough to even quote
it. But beginners usually need not worry about it.)

Terry J. Reedy


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class-level variables - a scoping issue John Nagle Python 22 10-15-2010 11:50 PM
Class-Level Instance Variables John W. Long Ruby 1 09-23-2006 12:31 AM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Class-Level Variables in Web Form Landley ASP .Net 2 09-20-2005 09:21 PM



Advertisments