[Tutor] Global values & import scope

Kent Johnson kent37 at tds.net
Wed Jan 24 12:06:27 CET 2007


Luke Paireepinart wrote:
> 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.

Yes, that's right.

>> 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

That should be Foo.itemID += 1; the class namespace is not part of the 
default name search path.

Kent



More information about the Tutor mailing list