p-robot
p-robot

Reputation: 4904

Perform method when an attribute is added to a class in Python

I would like to instantiate a class, say, Fighter with and without the attribute weapon_size. But when the attribute weapon_size is set (either at instantiation or later) I want to run some method which adds another attribute damage (and perhaps does some other calculations). I'm trying to do this by overloading the setattr function as below.

My question: is this the correct [and sensible] way to do this?

class Fighter(object):
    def __init__(self, prenom, weapon_size = None):
        self._prenom = prenom
        self._weapon_size = weapon_size
        if not weapon_size is None:
            self.AddWeapon(weapon_size)

    def __setattr__(self, name, value):
            if name == "weapon_size":
                print "Adding weapon"
                self.AddWeapon(value)
                self.__dict__['_' + name] = value
            else:
                self.__dict__[name] = value

    def AddWeapon(self, x):
        self._damage = x/2.

    @property
    def weapon_size(self):
        "Weapon size"
        return self._weapon_size
    @property
    def prenom(self):
        "First name"
        return self._prenom

U = Fighter('Greg')
U.weapon_size = 9
print U.weapon_size, U._damage
>> 9 4.5
W = Fighter('Jeff', weapon_size = 11)
print W.weapon_size, W._damage
>> 11 5.5

Upvotes: 1

Views: 50

Answers (1)

dursk
dursk

Reputation: 4445

You can define a custom setter for the specific property.

@weapon_size.setter
def weapon_size(self, value):
    # custom code here
    self._weapon_size = value

EDIT: But really, at the end of the day, I think it's much cleaner for you to simply do:

class Fighter(object):
    def __init__(self, prenom, weapon_size=None):
        ...
        self.weapon_size = weapon_size
        ...

    @property
    def damage(self):
        if self.weapon_size:
            return self.weapon_size / 2
        return None

Upvotes: 3

Related Questions