Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python - property() function

The property() function is used to define properties in the Python class.

The property() method in Python provides an interface to instance attributes. It encapsulates instance attributes and provides a property, same as Java and C#.

The property() method takes the get, set and delete methods as arguments and returns an object of the property class.

It is recommended to use the property decorator instead of the property() method.

property(fget, fset, fdel, doc)

Parameters:

  1. fget: (Optional) Function for getting the attribute value. Default value is none.
  2. fset: (Optional) Function for setting the attribute value. Default value is none.
  3. fdel: (Optional) Function for deleting the attribute value. Default value is none.
  4. doc: (Optional) A string that contains the documentation. Default value is none.

Return Value:

Returns the property attribute from the given getter, setter, and deleter.

The following example demonstrates how to create a property in Python using the property() function.

Example: property()
class person: def __init__(self): self.__name='' def setname(self, name): print('setname() called') self.__name=name def getname(self): print('getname() called')return self.__name name=property(getname, setname)

In the above example, property(getname, setname) returns the property object and assigns it to name. Thus, the name property hides the private instance attribute __name. The name property is accessed directly, but internally it will invoke the getname() or setname() method, as shown below.

Example: property()
>>> from person import person >>> p1=person() >>> p1.name="Steve" setname() called >>> p1.name getname() called 'Steve'

As you can see above, the getname() method gets called automatically when we access the name property. In the same way, the setname method gets called when we assign a value to the name property. It also hides the instance attribute __name.

In the same way, you can specify a deleter method for the property, as shown in the below script.

Example: property()
class person: def __init__(self, name): self.__name=name def setname(self, name): print('setname() called') self.__name=name def getname(self): print('getname() called')return self.__name def delname(self): print('delname() called')del self.__name # Set property to use get_name, set_name # and del_name methods name=property(getname, setname, delname)

The delname() function would be invoked when you delete the name property.

Example: property()
>>> from person import person >>> p1=person() >>> p1.name="Steve" setname() called >>> del p1.name delname() called

In this way, we can define a property in the class using the property() function in Python.

@property decorator makes it easy to declare a property instead of calling the property() function. Learn about it next.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.