[Tutor] class variables

Nick Lunt nick at javacat.f2s.com
Thu Jun 24 18:37:47 EDT 2004


Thankyou chad. I replied to this yesterday but it has yet to appear on the
list.

Thanks for your help.

There is definitely something wrong with the list at the moment.

Its now 2330 in the UK, Ill see what time this gets to the list.

Nick.


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of Chad Crabtree
Sent: 24 June 2004 06:13
To: tutor at python.org
Subject: Re: [Tutor] class variables


> 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 ?
>
this troubled me at first and I'm not sure but I may have asked this
very question here.

self.var in this case means that it's a class variable available to
the instance.  In this case 'cans' is a function variable only
available to things inside that function.  just like this

x=10

def dosomething():
  x=20
  print x
print x

>>dosomething()
20
>>print x
10


I hope this helps.



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list