[Tutor] Global values & import scope

Kent Johnson kent37 at tds.net
Wed Jan 24 15:31:11 CET 2007


Wesley Brooks wrote:
> Thanks for your help I tested what you two said as follows and it
> worked great. Thank you.
> 
> (Bellow in file TEST_ClassID.py)
> class AssemblyItem:
>     itemID = 0
>     def __init__(self):
>         self.ID = "assemblyItem" + str(AssemblyItem.itemID)
>         AssemblyItem.itemID += 1
> 
>     def ReturnID(self):
>         return self.ID
> 
> (Bellow in file TEST_ImportID1.py)
> from TEST_ClassID import AssemblyItem
> 
> class Parent1:
>     def __init__(self):
>         self.testList = []
> 
>     def PrintID(self):
>         self.testList.append(AssemblyItem())
>         print self.testList[-1].ReturnID()

That's a bit convoluted if all you are trying to do is test 
AssemblyItem. You could just write

from TEST_ClassID import AssemblyItem

def printID():
	item = AssemblyItem()
	print tem.ReturnID()

or even

def printID():
	print AssemblyItem().ReturnID()

Kent



More information about the Tutor mailing list