Setting a Global Default for class construction?
Josh English
english at spiritone.com
Sat Feb 1 14:25:25 EST 2003
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.
Josh English
english at spiritone.com
Chad Netzer wrote:
>
> On Fri, 2003-01-31 at 13:58, Josh English wrote:
> > 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
> >
> > I thought that this would work if I called:
> >
> > >>>SetThing('hallo')
> > >>>a = NewThing()
> > >>>a.Thing
> > 'it'
> >
> > I would expect a.Thing to return 'hallo', not 'it'. Is this possible to
> > do in Python?
>
> It does return 'hallo' for me, in Python 2.2, 2.1, and 1.5. Are you
> sure what you posted is exactly what you have tested? In particular,
> the "global _Thing" in SetThing() is crucial.
>
> Of course, globals are evil. :) Couldn't you just do:
>
> a = NewThing( 'hallo' )
>
> and live happily (you'd have to fix the init code, btw)?
>
> --
> Bay Area Python Interest Group - http://www.baypiggies.net/
>
> Chad Netzer
> (any opinion expressed is my own and not NASA's or my employer's)
More information about the Python-list
mailing list