2013-12-08 208 views
1

我有我的商務類的實例列表。我習慣於在類的頂部定義一個類的變量。 Business類中的變量是一個標籤列表。當我瀏覽商家名單時,有些商店有標籤,有些則沒有。在20個企業中,列表中的第4個元素有4個標籤。在將這些標籤添加到此業務後,所有以下Business的實例也會共享這些標籤。這是我的講座Python中的全局變量?

from tag import * 

class Business: 
    name = "" 
    website = "" 
    phone = "" 
    address = "" 
    city = "" 
    state = "" 
    postalCode = "" 
    tags = [] 
    data = {} 

    def __init__(self, name): 
     self.setName(name) 

    # Modifiers 

    def setName(self, name): 
     self.name = name 

    def setWebsite(self, website): 
     self.website = website 

    def setPhone(self, phone): 
     self.phone = phone 

    def addTag(self, Tag): 
     self.tags.append(Tag) 

    def setAddress(self, address): 
     self.address = address 

    def setCity(self, city): 
     self.city = city 

    def setState(self, state): 
     self.state = state 

    def setPostalCode(self, postalCode): 
     self.postalCode = postalCode 

    def set(self, key, value): 
     self.data[key] = value 

    def unset(self, key): 
     del self.data[key] 

    # Accessors 

    def getWebsite(self): 
     return self.website 

    def getName(self): 
     return self.name 

    def getPhone(self): 
     return self.phone 

    def getTags(self): 
     return self.tags 

    def getAddress(self): 
     return self.address 

    def getCity(self): 
     return self.city 

    def getState(self): 
     return self.state 

    def getPostalCode(self): 
     return self.postalCode 

    def get(self, key): 
     return self.data[key] 

    def getKeys(self): 
     return self.data.keys() 

    # Helpers 

和標籤被添加到一個企業就像這個 -

if len(categories) > 1: 
    for cat in categories: 
     B.addTag(Tag(cat)) 

在我的商業類的全球商務的所有實例的頂部定義的變量?我該如何解決這個問題?

+1

這是一個非常普遍的問題。請參閱[此處](http://stackoverflow.com/a/15489578/748858)以獲取解釋。 – mgilson

+0

或者,[這裏](http://stackoverflow.com/q/13070948/748858)是另一個。 – mgilson

+0

而這一個:http://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes –

回答

0

是的,這樣分配的mutables對班級來說是「全局」的。爲了使它們成爲「本地」,將它們定義爲實例變量。要爲標籤做到這一點,例如,刪除你有全局定義,並添加,而不是一行__init__像:

def __init__(self, name): 
    self.setName(name) 
    self.tags = [] 

這個標籤是價值分配給self而不是類一般。

0

你應該做這種方式來代替:

from tag import * 

class Business: 
    def __init__(self, name, website='', phone='', address='', city='', state='', postal_code='', tags=None, data=None): 
     self.name  = name 
     self.website  = website 
     self.phone  = phone 
     self.address  = address 
     self.city  = city 
     self.state  = state 
     self.postal_code = postal_code 

     self.tags  = [] 
     if tags is not None: 
      for tag in tags: 
       self.add_tag(tag) 

     self.data  = {} if data is None else data 

    def add_tag(self, tag): 
     if not isinstance(tag, Tag): 
      tag = Tag(tag) 
     self.tags.append(tag) 

    def add_data(self, key, value): 
     self.data[key] = value 

    def remove_data(self, key): 
     del self.data[key] 

    def get_data(self, key): 
     return self.data[key] 

    def get_data_keys(self): 
     return self.data.keys() 

的Python通常避免getter和setter方法,除非需要進行一些額外的處理或錯誤檢查

+0

好吧,工作。但是什麼不使用獲取和setter方法?添加這個抽象層不是一個好習慣嗎? –