[Tutor] class variables

Nick Lunt nick at javacat.f2s.com
Wed Jun 23 17:33:30 EDT 2004


Magnus and Danny, thankyou very much for your explanations.

>From what you've both told me, it would seem that putting a local variable
in the __init__ method
of a class is quite pointless, ie

example 1
>>>class beer:
>>>	def __init__(self):
>>>		cans = 4
>>>		self.make = 'murphys'

The variable 'cans' will not be useable in any invocation of the class beer.
When I try to use the class in example 1, I get this -

>>> b1 = beer()
>>> b1.cans
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: beer instance has no attribute 'cans'


When I put the cans variable outside of the init method, ie

example 2
>>>class beer:
>>>	cans = 4
>>>	def __init_(self):
>>>		self.make = 'murphys'

then instances of the class 'beer' will be able to set and get
instance.cans, just as if it was coded as 'self.cans'.
Here's what I get with the version of the 'beer' class in example 2 -

>>> b2 = beer()
>>> b2.cans
4
>>> b3 = beer()
>>> b3.cans
4
>>> b2.cans = 10
>>> b2.cans
10
>>> b3.cans
4
>>>

Which is how Magnus showed me.

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

But also, I can see no reason to use a class variable outside of a class
method with no self prefix.

I admit Im quite confused by this still. Many python programs Ive studied
mix up class variable with and without the 'instance/self' prefix.

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.

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

Many thanks,
Nick.






-----Original Message-----
From: Magnus Lycka [mailto:magnus at thinkware.se]
Sent: 23 June 2004 20:55
To: Nick Lunt
Cc: tutor at python.org
Subject: Re: [Tutor] class variables


Nick Lunt wrote:
> Hi folks,
>
> Im making my way through the book 'Learning Python 2nd Edition'. I
> understand classes but I have a question.
> Take the following for example -
>
> class beer:
>     def __init__(self):
>        self.make = 'murphys'
>        cans = 4
>
> There's several examples I've seen where 'self.var' isn't used, just
> 'var' is, as in 'cans' above.
> I know that each instance of 'beer' will have its own value for 'make'
> but what is the use of 'cans' without using self ?

It's simply a local variable in the function. In this case it's
completely pointless, since you just assign to it and then the
function ends! Compare it with

def init():
    global make
    make = 'murphys'
    cans = 4

Note that class variables, i.e. variables defined in the scope of the
class, is something completely different. Like this:

class Beer:
    cans = 4
    def __init__(self):
        self.make = 'murphys'
        a_local = 5

The class varaible Beer.cans is shared between all instances of the class.
There is a separate instance variable self.make for each instance of the
Beer class. The local varable only exists during the duration of the
execution of the __init__ method. Note that instance varables hide class
varaibles.

>>> class Beer:
        cans = 4
        def __init__(self):
                self.make = 'murphys'


>>> a = Beer()
>>> b = Beer()
>>> a.make, a.cans
('murphys', 4)
>>> b.make, b.cans
('murphys', 4)
>>> a.make = 'Utenos'
>>> a.make, a.cans
('Utenos', 4)
>>> b.make, b.cans
('murphys', 4)
>>> Beer.cans = 6
>>> a.make, a.cans
('Utenos', 6)
>>> b.make, b.cans
('murphys', 6)
>>> b.cans = 3
>>> b.make, b.cans
('murphys', 3)
>>> del b.cans
>>> b.make, b.cans
('murphys', 6)
>>>

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