Home > front end >  The uses of property (fget , fset)
The uses of property (fget , fset)

Time:02-28

Hello I'm beginner with Python and I have some difficulties to understand the uses of properties:

class Weapon:
    def __init__(self, name, damages, sell_price):
        self.name = name
        self.damages = damages
        self.sell_price = sell_price


class Player:
    def __init__(self, name, level=1, golds=5, HP=100, MP=100, EXP=0, weapon=Weapon("Epee en bois", 32, 1)):
        self._name = name
        self.level = level
        self.golds = golds
        self.HP = HP
        self.maxHP = HP
        self.MP = MP
        self.maxMP = MP
        self.EXP = EXP
        self.next_level_EXP = 100
        self.weapon = weapon

    def get_name(self):
        return self._name

    def set_name(self, name):
        if len(name) > 16:
            raise Exception("name should have less than 16 character.")

        self._name = name
        print("your name is changed to: -> {}".format(self.name))

    name = property(fget=get_name, fset=set_name)
    

ask_player_name = True

while ask_player_name:
    try:
        player_name = input("Choisir un nom (16 caracteres max.) : ")
        hero.name = player_name
        ask_player_name = False
    except Exception as message:
        print(message)

So, for example, for the code above I understand the part where they use set_name, it's to correct if there is a name with more than 16 characters. For the use of get_name, I didn't get it at all. It seems useless

If you could, please explain me and orient by giving some examples for some common application of property.

CodePudding user response:

What you are seeing in this code is an old Java-style getter/setter pattern. When the author declares

name = property(fget = get_name, fset = set_name)

the .name attribute is attached Player class as a property. It is accessed using the get_name and set using the set_name methods. The more Pythonic way of writing this would be:

class Player:

    def __init__(self, name, level=1, golds=5, HP=100, MP=100, EXP=0, weapon=Weapon("sword", 32, 1)):
        self._name = name
        self.level = level
        self.golds = golds
        self.HP = HP
        self.maxHP = HP
        self.MP = MP
        self.maxMP = MP
        self.EXP = EXP
        self.next_level_EXP = 100
        self.weapon = weapon

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        if len(name) > 16:
            raise Exception("name should have less than 16 character.")

        self._name = name
        print("your name is changed to: -> {}".format(self.name))

CodePudding user response:

A getter is beneficial if you need to refactor the internal representation of your data. You currently just store the name in self._name; the getter allows you to change this without affecting all users of your class, as it would be the case when they'd access the field directly, because you'd adapt the getter at the same time.

  • Related