Newbie asks about static variables...

Carel Fellinger cfelling at iae.nl
Tue May 4 19:35:22 EDT 1999


Jeremy Hylton <jeremy at cnri.reston.va.us> wrote:
> A slight variant, which uses a little less magic, is to make
> killParrot a method on an object that keeps track of the state.

> class Spam:
>     def __init__(self):
>         self.no_calls = 0
>     def addEggs(self):
>         self.no_calls = self.no_calls + 1

> meal = Spam()
> meal.addEggs()

just to add some magic, you could as wel do:
  killParrot = Spam().addEggs
  killParrot()
and the rest of your program doesn't need to change:)

Bytheway, a second instance created like in
    ResurrectParrot = Spam().addEggs
would start counting from zero. so you had better use
a class variable here. But hey, I'm a newbie too, so
I probably lure you in beleiving the wrong things:)
(O the joy of being new to Python just makes
 me forget I ever had to program in C)

-- 
groetjes, carel




More information about the Python-list mailing list