类中的Python全局变量?

时间:2013-12-08 21:28:16

标签: python class variables global

我有一个Business类的实例列表。我习惯于在类的顶部为类定义变量。 Business类中的变量是一个标记列表。当我遍历企业列表时,有些人会有标签,有些则没有。在20个企业中,列表中的第4个元素有4个标签。将这些标记添加到此业务后,所有以下Business业务实例也会共享这些标记。这是我的商务课 -

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

在我的业务类顶部定义的变量是全局的吗?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

是的,以这种方式分配的可变对象是“全局的”。要使它们“本地”,将它们定义为实例变量。要对标记执行此操作,例如,删除您拥有的全局定义,并添加一行到__init__,如:

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

这会将标签的值分配给self,而不是一般地分配给班级。

答案 1 :(得分: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