global declarations

Frederik Fuest ffrederik at gmx.de
Mon Dec 23 18:05:41 EST 2002


> x = 10
> class ManagerOfX:
>     def printIt(self):
>         global x
>         print x
>         x = x + 1
> 
>     def restoreIt(self):
>         global x
>         x = 10
> 
> m = ManagerOfX()
> m.printIt()
> m.printIt()
> m.restoreIt()
> m.printIt()
> 
> 
>   Global variables lock you into some bad patterns, make code hard to
> reuse/scale.  Avoid their use where possible.  For example, the above
> should
> be rewritten as...
> 
> class ManageOfX:
>     x = 10
> 
>     def printIt(self):
>         print self.x
>         self.x = self.x + 1
> 
>     def restoreIt(self):
>         self.x = 10


thank you very much, this was exactly was i needed to know!
now i've killed almost all global variables in my code, and it runs! 

regards, 

Frederik

-- 
+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!





More information about the Python-list mailing list