Threads + Enviroment Variables

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Sep 30 11:59:05 EDT 2001


"Adonis Vargas" <deltapigz at telocity.com> writes:

> now this is not the real code im working on; since the code im
> working on is very complex i chose not to paste it; just provided
> the general idea of what i am trying to accomplish, therefore the
> code provided could not be correct.

Unfortunately, the example you gave does not give the general idea.
It is not clear whether the various occurences of 'something' are
meant to be the same thing in all places, or different things.

In any case, I think threads have nothing to do with that: your code
would likely fail if you just called the function directly, instead of
running it in a thread.

> -- in module something.py --
> something = []
> 
> -- in script --
> from something import * # import the variable 'something' right?

Wrong. It imports the value of the variable something.something, and
binds it to a local name 'something'. So if you do now

something = 1

then something.something won't change. OTOH, if you do

something.append(1)

then this modifies the list object, rather than assigning to a
variable, so it *will* change something.something as well.


> def somefunction(somevalue):
>     return somevalue+=1
> 
> def somethreadedfunction():
>     for i in range(10):
>         something = somefunction(0) # boom. an 'undefined' error???

What exactly is it complaining about? That somefunction is not known?
That would be very strange.

Perhaps you notice that the global variable something is not assigned
by this statement. This is intentional, if you want to modify the variable,
you need to do

     global something

However, this still won't change something.something.

Regards,
Martin



More information about the Python-list mailing list