0

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
Community
  • 1
  • 1
AtomicCrash
  • 81
  • 1
  • 5

3 Answers3

0

This could probably be a class method. class methods recieve the class as the first argument (not an instance of the class like regular methods). Doing it this way you'll also need to return the value so the caller can get access to ie.

class Id_Class(object):    
    new_id   = itertools.count(1000).next
    order_id = itertools.count(1000).next
    person_id= itertools.count(1000).next

    @classmethod
    def new_id(cls, t):   # t = type of id required
        if t == "Order":
            return cls.order_id()
        elif t == "Person":
            return cls.person_id()
        else:
            raise ValueError("Must be 'Order' or 'Person'")

Although you really don't need a class here at all:

new_id   = itertools.count(1000).next
order_id = itertools.count(1000).next
person_id= itertools.count(1000).next
def new_id(t):   # t = type of id required
    if t == "Order":
        return order_id()
    elif t == "Person":
        return person_id()

which can then be called simply by:

my_order_id=new_id('Order')
my_person_id=new_id('Person')
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • so i don't need to call it by: Id_Class.new_id('Order')? Ha, just tried it and answered my own question :) – AtomicCrash Jul 12 '12 at 22:12
  • @user1386172 The advantage of using the class is that it keeps your namespace slightly more clear -- but you could change the variable name `order_id` to `_order_id` and that would probably be good enough. – mgilson Jul 12 '12 at 22:20
0

Because you're calling new_id like a static method, try adding @staticmethod instead like:

class Id_Class(object):    
    new_id   = itertools.count(1000).next
    order_id = itertools.count(1000).next
    person_id= itertools.count(1000).next

    @classmethod
    def new_id(cls, t):   # t = type of id required
        if t == "Order":
            return Id_Class.order_id()
        elif t == "Person":
            return Id_Class.person_id()
Marconi
  • 3,601
  • 4
  • 46
  • 72
  • Why make it a classmethod if you're not going to use `cls`? Might as well make it a staticmethod and get rid of the `cls` argument. – mgilson Jul 12 '12 at 21:42
0

You need a @staticmethod decorator on the function declaration.

class Id_Class(object):    
    new_id   = itertools.count(1000).next
    order_id = itertools.count(1000).next
    person_id= itertools.count(1000).next

    @staticmethod
    def new_id(t):   # t = type of id required
        if t == "Order":
            return Id_Class.order_id()
        elif t == "Person":
            return Id_Class.person_id()
Eric
  • 95,302
  • 53
  • 242
  • 374
  • It still returns `None` (which is clearly not intended), and it's only called with 1 argument, so there will be an exception raised because it's called with the wrong number of arguments. – mgilson Jul 12 '12 at 21:38
  • if you're using Eric's code, don't assign id to self but instead return it directly since you're assigning it to Order's uid. – Marconi Jul 12 '12 at 21:40