[Tutor] class variables

Chad Crabtree flaxeater at yahoo.com
Thu Jun 24 01:12:40 EDT 2004


> 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 



More information about the Tutor mailing list