I am learning OOP in python and have come across this code:
class Person:
def __init__(self, first_name):
self.first_name = first_name
# Getter function
@property
def first_name(self):
return self._first_name
# Setter function
@first_name.setter
def first_name(self, value):
if not isinstance(value, str):
raise TypeError('Expected a string')
self._first_name = value
This code works, even after the fact that the attribute we are setting is called firstname
and what we are getting is called _firstname
. I want to know whether these 2 attributes are same in python or are they to be considered different? And why does this code work even after such seeming incongruency?