By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Class level variables in Python

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Brian Munroe" <bm*****@tribador.nu> wrote in message
news:c1**************************@posting.google.c 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
Jul 18 '05 #2

P: n/a
"Brian Munroe" <bm*****@tribador.nu> wrote in message
news:c1**************************@posting.google.c 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
Jul 18 '05 #3

P: n/a
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(8) == 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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.