Re: RE: [Tutor] class variables

Magnus Lycka magnus at thinkware.se
Thu Jun 24 16:03:51 EDT 2004


Nick Lunt wrote:
> As it stands now I cant see any use for a local variable inside the __init__
> method, ie a variable without the self prefix.

You use local variables for temporary storage of data that you only
need during the run of the function. Whether it's __init__, another
method in a class or a plain function doesn't matter.

Look here:

>>> def avg(values):
        n = len(values)
        sum = 0.0
        for value in values:
                sum += value
        return sum / n

>>> print avg([1,2,3,4,5])
3.0

The function avg contains two local variables, n and sum. These are
lost as soon as avg exits. Are they useless? Nope, they serve a
purpose, and when they are done doing that, we discard them.
 
> But also, I can see no reason to use a class variable outside of a class
> method with no self prefix.

The point with a class variable is that it can be used to share
information between all the instances of a class. A class attribute
exists in the scope of the class. There is just one copy of this
variable, regardless of how many instances of the class you create.

Let's imagine that you have a class in a GUI system which described
some kind of widget, maybe some kind of button. If you have a variable
which determines the color of the button, and you have decided that
all buttons on the screen must always be the same color, then this
variable belongs in the scope of the class. If you want different
buttons to be able to have different colors, you want this variable
in the scope of the instance object, i.e. under "self".
 
> I admit Im quite confused by this still. Many python programs Ive studied
> mix up class variable with and without the 'instance/self' prefix.

I hope the examples above help you understand what these different
scopes are used for. Where a variable is defined should help you
understand what it's used for, and when it contains anything of
interest.
 
> Im also confused about whether I should put my class variable inside a
> method and use the self prefix, leave them outside of a method with no
> prefix, or leave them outside of a method but give them a prefix.

I'm wondering if you should use any classes at all...
 
> With the simple programs I write at the moment, it wouldnt really make much
> difference how I did it in most cases, but I would like to know when I
> should or shouldn't be using self. And when I should be putting class
> variables outside a class method (if ever).

I hope you already grokked the purpose of local variables from the
example above.

I'll make a silly example using both instance and class variables here! :)

>>> class Animal:
        def __init__(self, weight):
                assert hasattr(self.__class__, 'legs'), 'Concrete animals need legs'
                self.weight = weight
        def weight_per_leg(self):
                return self.weight/self.legs


>>> class Insect(Animal):
        legs = 6


>>> class Spider(Animal):
        legs = 8


>>> i = Insect(0.001)
>>> print i.weight_per_leg()
0.000166666666667
>>> s = Spider(0.004)
>>> print s.weight_per_leg()
0.0005

You see? All spiders have eight legs (unless you pull some off).
Each spider has an individual weight. Number of legs is an
attribute of the class spider, and weight is an attribute of the
individual spider instance.
 
> I apologise for not grasping it very quickly. My previous experiences with
> classes comes from java, and that was a while ago, and java classes seem a
> lot stricter than python classes.

Ok. Are you sure that you really want to use classes for the code
you are writing? Java has this stupid attitude that everything should
be shoehorned into a class whether it fits there or not, as if designs
would become better if they are in a class. It's a bit like believing
that cars run faster if we paint them red, just because Ferraris are 
red.

In Python there is no reason to use a class unless you want to use
some of the benefits that classes give. 

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list