Day #60 - Getters and Setters in Python

Day #60 - Getters and Setters in Python

ยท

3 min read

Introduction

Welcome to my 60th blog post on the Python journey. On day 60, I learned about Getters and setters method in Python. These methods help us to provide additional functionality to access the values object properties. Let's dive into more details and understand these methods in Python.

So let's get started......

Getters

  • These methods are used to access the values of an object's properties.

  • Getters do not take any value.

  • They are defined using the decorator @property

  • Getters return the value of a specific property,

Example -

In the below example, MyClass class has a single property, 'value*', which is initialized in the **init method.* The value method is defined as a getter using the @property decorator,** and is used to return the value of the value property.

To use the getter, we can create an instance of the MyClass class, and then access the value property as if it were an attribute:

class MyClass:
    def __init__(self, value):
        self._value = value

    @property
    def value(self):
        return self._value

obj = MyClass(10)
obj.value

Output

10

Setters

  • Note that the getters do not take any parameters and we cannot set the value through the getter method.

  • Hence we make use of the setter method

  • The setter method can be added by decorating the method with @property_name.setter

Example -

class MyClass:
    def __init__(self, value):
        self._value = value

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, new_value):
        self._value = new_value

obj = MyClass(10)
obj.value = 20

More Example -

class MyClass:
  def __init__(self, value):
      self._value = value

  def show(self):
    print(f"Value is {self._value}")

  @property
  def ten_value(self):
      return 10* self._value

  @ten_value.setter
  def ten_value(self, new_value):
      self._value = new_value/10

obj = MyClass(10)
obj.ten_value = 67
print(obj.ten_value)
obj.show()

Output -

67.0
Value is 6.7

Note -

Getters provide a convenient way to access the values of an object's properties while keeping the internal representation of the property hidden.

Getter and setter methods can be useful for encapsulation and data validation.

Resources Used

You can watch the video of Day#60 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 60, I learned about the special method that can be used to access an object's properties and can be used in encapsulation and data validation.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #60

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย