Setting a Global Default for class construction?

Mongryong Mongryong at sympatico.ca
Sat Feb 1 16:36:25 EST 2003


On Sat, 2003-02-01 at 14:30, Josh English wrote:
> Lao Coon wrote:
> > Josh English <english at spiritone.com> wrote in 
> > news:b1erj009ok at enews3.newsguy.com:
> > 
> > 
> >>Here is the code that I am struggling with in a Python module:
> >>
> >>_Thing = "it"
> >>
> >>def SetThing(s):
> >>     global _Thing
> >>     _Thing = str(s)
> >>
> >>class NewThing:
> >>     def __init__(self,thing=_Thing):
> >>          self.Thing = _Thing
> > 
> > 
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'






More information about the Python-list mailing list