Setting a Global Default for class construction?
Josh English
english at spiritone.com
Sun Feb 2 19:00:46 EST 2003
Mongryong wrote:
> Ah, this is one of Python's 'Gotchas!!' that will blow of your leg if
> you try to get to fancy with your code.
>
> Default values for parameters are set only once - at import time. The
> following is what I believe you're looking for:
>
> Thing="it"
> class NewThing:
> def __init__ (self, thing=None):
> if thing is None:
> thing = Thing
> self.thing = thing
>
>
>>>>module.Thing = "new it"
>>>>t = module.NewThing()
>>>>t.thing
>
> 'new it'
>
>
I suspected I was a getting got by a Gotcha! I had to use a global
statement:
_Thing = 'it'
class NewThing:
def __init__(self,thing=None):
global _Thing
if thing: self.Thing = thing
else: thing = _Thing
Thanks everyone for the help. I am glad that such a simple solution was
to be had.
Josh English
english at spiritone.com
More information about the Python-list
mailing list