[Tutor] Scope and elegance
shlomme at gmx.net
Mon Jan 7 21:58:19 CET 2008
I'll try to swallow down the "Globals are eevil" comment, there's enough
literature on that already.
Maybe I'm spoiled from programming too much Java in the last year, but
IMHO it's a good idea to put the singleton instance into the class
itself rather than into some module.
This way, you (can) make sure that all accesses to the class really go
to the same instance.
There are, of course, many ways to do that, but I'd prefer a method on
cls._inst = Foo()
You can also make more advanced singletons with overwriting __new__ etc,
but that probably overdoes it for the case at hand.
> Is it unPythonic of me to:
> a) Want to create the Board instance inside the main() function
> b) Want the Board instance to be globally available so that
> Counters and other objects can talk to it directly?
> I am not so much concerned in getting this to work (I can already do
> that); I am more concerned with understanding how to treat this in the
> most Pythonesque way.
IMHO it would be better to hand in the board instance into the counter
instances. Globals add hidden dependencies, make code harder to test
etcetc. (Swallowing down didn't work ;-)
Torsten Marek <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146 894C 7CC5 451E A244 C858
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080107/ae73728d/attachment.pgp
More information about the Tutor