0

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?

Gsbansal10
  • 303
  • 5
  • 12
  • It's meant to represent a "private" member, but python does not have true private members. – Maximilian Burszley Jan 10 '19 at 17:12
  • 1
    You need to understand what `property` is doing, the fact that `first_name` and `_fist_name` distinct only by a single underscore is irrelevant. Both are normal variable names. The key issue is the property. The single underscore is *merely a convention* signaling that the variable is not part of the public interface – juanpa.arrivillaga Jan 10 '19 at 17:13

0 Answers0