Re: [Tutor] Global values import scope

2007-01-24 Thread Luke Paireepinart
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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global values import scope

2007-01-24 Thread Kent Johnson
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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global values import scope

2007-01-24 Thread Kent Johnson
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?
 
 itemID = 0
 class AssemblyItem:
 def __init__(self):
 global itemID
 self.ID = assemblyItem + str(itemID)
 itemID += 1

That will work fine. When a module is imported twice, the second import 
received a cached copy of the same module; the module is only 
instantiated once. The variable itemID will just exist in one place, in 
the single instance of the module, and AssemblyItems created from 
different clients will all share the same counter.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global values import scope

2007-01-24 Thread Wesley Brooks
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()

(Bellow in file TEST_ImportID2.py)
from TEST_ClassID import AssemblyItem

class Parent2:
def __init__(self):
self.testList = []

def PrintID(self):
self.testList.append(AssemblyItem())
print self.testList[-1].ReturnID()

(Bellow, the commands run in the python terminal in the same directory)
 from TEST_ClassID1 import Parent1
from TEST_ClassID2 import Parent2
a = Parent1()
b = Parent2()
a.PrintID()
assemblyItem0
a.PrintID()
assemblyItem1
b.PrintID()
assemblyItem2
b.PrintID()
assemblyItem3
a.PrintID()
assemblyItem4
b.PrintID()
assemblyItem5

Thanks again for your help.

Wesley Brooks.


On 24/01/07, Kent Johnson [EMAIL PROTECTED] wrote:
 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?
 
  itemID = 0
  class AssemblyItem:
  def __init__(self):
  global itemID
  self.ID = assemblyItem + str(itemID)
  itemID += 1

 That will work fine. When a module is imported twice, the second import
 received a cached copy of the same module; the module is only
 instantiated once. The variable itemID will just exist in one place, in
 the single instance of the module, and AssemblyItems created from
 different clients will all share the same counter.

 Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global values import scope

2007-01-24 Thread Kent Johnson
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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor