[Tutor] Global values & import scope
Luke Paireepinart
rabidpoobear at gmail.com
Wed Jan 24 11:49:58 CET 2007
Wesley Brooks wrote:
> Dear Users,
>
> I'm using global values to create a unique ID, or name for each
> instance of a class. If I import the following in one file and test it
> it works fine. If the following class was imported in two different
> files run by the same program would each instance of the class have a
> unique name, or would they only be unique within the scope of the file
> which contains the import of the bellow class?
>
I believe that, if your program is importing 2 other packages, each of
which import some other package,
that other doubly-imported package will only be executed once, by
whichever one you import first.
> itemID = 0
> class AssemblyItem:
> def __init__(self):
> global itemID
> self.ID = "assemblyItem" + str(itemID)
> itemID += 1
>
I'm 99% sure you can accomplish the same thing with a variable that is
global to all instances of the class.
something like (may not work):
class Foo:
itemID = 0
def __init__(self):
itemID += 1
HTH,
-Luke
More information about the Tutor
mailing list