PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

[FIXED] when to use getter and setter with property?

 October 26, 2022     oop, properties, python, python-3.x     No comments   

Issue

When should you use a property with getters/setters? It is not pythonic or wrong to not use a property with getters and setters? Should or shouldn't I write it with a property?

Examples:

class Person:
    def __init__(self, firstname, lastname, age):
        self.firstname = firstname
        self.lastname = lastname
        self.age = age

    def say_hi(self):
        print(f"""Hi i'm {self.firstname} {self.lastname} and i'm {self.age}""")

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, newage):
        if not isinstance(newage, int):
            raise TypeError("Expect an Integer")
        self._age = newage

versus

class Person2:
    def __init__(self, firstname, lastname, age):
        self.firstname = firstname
        self.lastname = lastname
        self.age = age

    def say_hi(self):
        print(f"""Hi i'm {self.firstname} {self.lastname} and i'm {self.age}""")

    def get_age(self):
        return self.age

    def set_age(self, newage):
        if not isinstance(newage, int):
            raise TypeError("Expect an Integer")
        self.age = newage


Solution

You should generally prefer to use "protected" variables (such as those starting with _) with properties (not separate functions that users need to call, that's just clunky), as it confers some advantages. This encapsulation is very handy as it:

  • lets you control the internal data completely, such as preventing people entering ages like -42 (which they will do if they can); and
  • lets you change the underlying implementation in any manner you want, without affecting clients.

For example on that last point, you may want to maintain a separate structure of all names and simply store references to those names in your Person class. This can allow you to store many more names, as the surname "Von Grimmelshausen" would be stored once (in the separate structure) and as much smaller indexes in all the Person objects that use it.

You can then totally change the naive getter from:

@property
def surname(self):
    return self._surname

to:

@property
def surname(self):
    return self._surname_db[self._surname_index]

without any changes to clients.



Answered By - paxdiablo
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing