Python's property() class

Python's property() class

As we know that in languages like Java, C++, classes are supposed to contain private data attributes as a best coding practice. That's why we need getter and setter methods for accessing and mutating those attributes i.e.. those attributes cannot be accessed directly outside of the class.

But Python doesn't have the concept of public or private attributes, though it follows a convention of naming the private attributes starting with underscore (_). For example an attribute named status will be named _status to indicate other developers that the attribute is private and to be careful while changing its value.

Let us consider an example Employee class in Python below:

No alt text provided for this image
A sample employee class with an attribute (_status) indicated private.
Output:

DISABLED        

As you can see in the above example the _status attribute, though indicated as private can be pretty much accessed outside of the class and can be mutated. But what if you really want to impose restriction on how a data attribute is set based on some use case ?? This is the problem that is solved by the built-in property() class.

Working of the property class

Let us consider the same Employee class example in the below code snippet:

No alt text provided for this image
Implementation of property class
Output:

Setter called..
Setter called..
Getter called ..
DISABLED        

Let's analyse the working of above code snippet:

  1. The class now contains the getter and setter methods get_status() and set_status() respectively.
  2. There is now a class attribute called status which is an instance of a property class. The class takes in two parameters fget and fset assigned with callables get_status and set_status respectively.
  3. Now the employee object whenever tries to set the status class attribute, Python knows that it is the class attribute which is also an object of type property. Then Python invokes the callable which is set against the fset (set_status) and passes the new value to it.
  4. Similarly, when the object tries to get the status class attribute, Python knows that it is the class attribute which is also an object of type property. Then Python invokes the callable which is set against the fget (get_status).
  5. One thing to note is that accessing the class attribute status outside the class is done in pretty much the same way as it was done in the previous example with _status. This means it is not required to call the get_status() and set_status() methods explicitly.

When should we use the property class ?

Whenever there is a restriction on the way a data attribute of a class should be set, you can consider using the built-in property class.

Additional notes:

  • property class also takes other arguments like fdel and doc. fdel is assigned a callable which deletes the object's attribute. doc argument is assigned with a string that can be used to describe the purpose of using the property class.
  • @property decorator can be used to achieve the same task, but that's left out for upcoming article.

Thank you for reading till the end and I hope you learnt something from it. Please follow me on Linkedin for more such articles on Python.

要查看或添加评论,请登录

Aman Bhardwaj的更多文章

  • Creating Python Packages

    Creating Python Packages

    Introduction Once we have written our code in a Python module, it should be available to use. Now, the next step is to…

  • Python Modules - 1: The Basics

    Python Modules - 1: The Basics

    Welcome to my yet another LinkedIn article related to Python. We will try to understand what are Python modules.

  • A refresher on Compilers

    A refresher on Compilers

    Introduction For sure, compilers are one of the most complicated programs ever written. We have been writing code in…

  • Python Decorators - Part lll

    Python Decorators - Part lll

    This article is in continuation to my previous two articles on decorators. I would encourage you to read both of those…

    1 条评论
  • Python Decorators - Part ll

    Python Decorators - Part ll

    ..

    2 条评论
  • Python Decorators - Part I

    Python Decorators - Part I

    Introduction Decorators are one of the widely used concepts in Python. I have not encountered a Python codebase, yet…

    1 条评论
  • Variable Scoping and Closures in Python

    Variable Scoping and Closures in Python

    Introduction Variable scoping works almost the same way in most programming languages with small differences. Though it…

社区洞察

其他会员也浏览了