Python property() With Examples

Python property() function returns a property attribute. It’s mostly used to create a manageable class attribute.

Python property()

Python property() function syntax is:

  1. fget: function for getting the attribute value
  2. fset: function to set the attribute value
  3. fdel: function to delete the attribute
  4. doc: creates the docstring for the attribute to be used in help() function.

Let’s look at an example to create property attribute in a class.


Notice that Person name property is managed through specified methods and it’s using the _name private property of Person.

We can create a read-only attribute or non-deletable attribute using property function.

If we define property function as:

Then del will throw exception as AttributeError: can't delete attribute.

Similarly, if we define property attribute as:

The name will be read-only. If we try to set its value using"Pankaj" then exception will be thrown as AttributeError: can't set attribute.

Python Property Decorator

We can use @property decorator to achieve the same thing.


You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: