Hi,
I wanted to clarify a doubt related to getters and setters in a python class. As given in the course (https://www.educative.io/courses/learn-object-oriented-programming-in-python/m27v4PR52mG) getters and setters are class methods. While this in an option, I read about another option of using getters and setters (as properties) via @property decorator.
PFB the code an alternative code for the same example as used in the course: (this one is via @property decorator):
class User():
def __init__(self, username=None): # defining initializer
self.__username = username
@property
def username(self):
return (self.__username)
@username.setter
def username(self, x):
self.__username = x
Steve = User(‘steve1’)
print(‘Before setting:’, Steve.username)
Steve.username = ‘steve2’
print(‘After setting:’, Steve.username)
Question for the author: Out of the two approaches which one do you think is more suitable while responding in an interview?