I've read a bit about automating the creation of object ids, but still getting lost... I have tried to base the following code around the great example by Algorias here ...
What i'm trying to achieve is a class that is the resource for all new id requests. The logic is that if it is all in one place it should be easier to manage...
But when i got to set x to an instance of Order i get the following:
>>> x = Order()
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
x = Order()
File "C:\Python27\delete.py", line 17, in __init__
self.uid = Id_Class.new_id("Order")
TypeError: unbound method new_id() must be called with Id_Class instance as first argument (got str instance instead)
Any help would be greatly appreciated
import itertools
class Id_Class(object):
new_id = itertools.count(1000).next
order_id = itertools.count(1000).next
person_id= itertools.count(1000).next
def new_id(self, t): # t = type of id required
if t == "Order":
self.id = Id_Class.order_id()
elif t == "Person":
self.id = Id_Class.person_id()
class Order(object):
def __init__(self):
self.uid = Id_Class.new_id("Order")
self.cus='Test'
class Person(object):
pass