1

I have a list of instances of my Business class. I'm used to defining the variables for a class at the top of the class. On of the variables in the Business class is a list of tags. When I loop through the list of businesses some have tags, some don't. Out of the 20 businesses the 4th element in the list has 4 tags. After these tags are added to this business all following instances of Business also share these tags. Here is my Business class-

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

And a tag is added to a business like this-

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

Are the variables defined at the top of my business class global to all instances of Business? How do I fix this problem?

micah
  • 7,596
  • 10
  • 49
  • 90

2 Answers2

0

Yes, mutables assigned that way are "global" to the class. To make them "local," define them as instance variables. To do this for tags, for example, remove the global definition that you have and add instead a line to __init__ like:

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

This assigns that value of tags to self rather than to the class generally.

John1024
  • 109,961
  • 14
  • 137
  • 171
0

You should be doing it this way instead:

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 generally avoids getters and setters unless some extra processing or error-checking is required

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • Alright that works. But what not use getting and setter methods? Isn't it a good habit to add this layer of abstraction? – micah Dec 08 '13 at 22:00