2012-07-12 58 views
0

我已經讀了一些關於自動化對象id的創建,但仍然迷路了......我試圖將以下代碼以Algorias here ......創建一個類來生成ID

我想要實現的是一個類,它是所有新的id請求的資源。其中的邏輯是,如果是在同一個地方,應該更容易管理...

但是當我到X設置爲訂單的一個實例,我得到如下:

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

任何幫助將不勝感激

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 

回答

0

您需要在函數聲明上使用@staticmethod裝飾器。

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() 
+0

它仍然返回'None'(這顯然不是有意的),並且它只用1個參數調用,所以會引發異常,因爲它被調用的參數數量錯誤。 – mgilson 2012-07-12 21:38:07

+0

如果您使用的是Eric的代碼,請不要將id分配給self,而是直接將其分配給Order的uid。 – Marconi 2012-07-12 21:40:29

+0

@Marconi - 也擺脫'self'參數... – mgilson 2012-07-12 21:43:44

0

這可能是一個類的方法。類方法接收類作爲第一個參數(不像常規方法那樣是類的實例)。這樣做,您還需要返回值,以便調用者可以訪問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'") 

儘管你真的不需要一個類在這裏都:

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

然後可以通過簡單地稱爲:

my_order_id=new_id('Order') 
my_person_id=new_id('Person') 
+0

所以我不需要調用它:Id_Class.new_id('Order')?哈,只是試了一下,並回答了我自己的問題:) – AtomicCrash 2012-07-12 22:12:48

+0

@ user1386172使用該類的好處是,它使您的名稱空間稍微更清晰 - 但您可以將變量名稱order_id更改爲_order_id,夠好。 – mgilson 2012-07-12 22:20:21

0

因爲你調用NEW_ID像靜態方法,請嘗試添加@staticmethod來代替:

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() 
+0

如果你不打算使用'cls',爲什麼使它成爲classmethod?不妨將它作爲一種靜態方法並擺脫'cls'參數。 – mgilson 2012-07-12 21:42:33