Setting a Global Default for class construction?

Peter Abel p-abel at t-online.de
Mon Feb 3 17:47:50 EST 2003


"Josh English" <english at spiritone.com> wrote in message news:<mailman.1044127622.14603.python-list at python.org>...
> What I want is to be able to set a default that will be reflected in new
> class objects when they are created. As I tested this code yesterday it
> worked as I have written. Today it is not doing it the same way. It
> should be representational of what the real code is doing. Clearly it is not.
> 
> The problem is that creating these class objects is automated in my
> code. Here is a larger picture of what's going on:
> 
> I have a XML document with a tag like this:
> 
> <usething>hallo</usething>
> 
> this tag comes before a tag like this:
> 
> <thing title="thingone" key="thingtwo">
> 
> There are several thing tags in the XML.
> 
> The parser reads a minidom implementation of the XML and has the
> following functions:
> 
> do_usething(self,node):
> 	txt = node.ExtractText(node)  # Pulls the text out of the node's children
> 	SetThing(txt)
> 
> do_thing(self,node):
> 	thisthing = NewThing()
> 	# get attributes and add them to the Thing object
> 	# other processes
> 
> The do_usething function does get called and reads the appropriate text
> and changes the global variable _Thing. What's not happening is in the
> do_thing function, when NewThing() is called it's not pulling the
> current value of _Thing when it creates the class object.
> 
> I am loathe to change the initialization of NewThing because there are
> other attributes I will also be using this class manually in the IDE,
> not always automatically generating them from XML source.
> 
> I have tried a few other do_usething functions:
> 
> def do_usething(self,node):
> 	global SetThing
> 	txt = self.ExtractText(node)
> 	print "Global Thing set to:",txt
> 	SetThing(txt)
> 	
> def do_usething(self,node):
> 	global _Thing
> 	txt = self.ExtractText(node)
> 	print "Global Thing set to:",txt
> 	_Thing=txt
> 	print _Thing
> 
> These both work as expected. Testing these I find that the global value
> of _Thing is being changed. It's just not reflected in the do_thing
> function. The NewThing object created in the do_thing function has a
> Thing attribute of 'it'.
> 
> After writing all this, I realize I should have used a different
> example. All these things are driving me crazy.
Stay cool.

When I understand you well, you want to have some _Thing that 
will remain static but with the possibility to change it under certain
circumstances to act as a new initial value.
So must it really be a global or can it be a class.attribute ?
>>> class A:
... 	__thing='it'
... 	def __init__(self,Thing=A.__thing):
... 		self.Thing=Thing
... 	def set_thing(self,Thing):
... 		A.__thing=Thing
... 	def pr(self):
... 		print 'self.Thing=%s, A.__thing=%s'%(self.Thing,A.__thing)
... 		
>>> a=A()
>>> a.pr()
self.Thing='it', A.__thing='it'
>>> a.set_thing('Italy')
>>> a.pr()
self.Thing='it', A.__thing='Italy'
>>> b=A()
>>> b.pr()
self.Thing='it', A.__thing='Italy'
>>> b.set_thing('reset to it')
>>> b.pr()
self.Thing='it', A.__thing='reset to it'
>>> a.pr()
self.Thing='it', A.__thing='reset to it'
>>> c=A('MyThing')
>>> c.pr()
self.Thing='MyThing', A.__thing='reset to it'
>>> 

Regards
Peter




More information about the Python-list mailing list