Class level variables in Python

B

Brian Munroe

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:
z = 1
def __init__(self):
self.x = 2

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
 
M

mackstann

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.
 
S

Sean Ross

Brian Munroe said:
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:
.... attr = 1
.... def __init__(self):
.... self.attr = 2
....
HTH
Sean
 
T

Terry Reedy

Brian Munroe said:
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:
z = 1
def __init__(self):
self.x = 2


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top