程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Usage of @property in Python

編輯:Python

Catalog

1. What is? property

2.property Two methods of attribute definition

3. use property Instead of getter and setter Method

1. What is? property

Simply put, once a method in a class is @property decorate , You can call this method as you would call an attribute , It can simplify the process for the caller to obtain data , And don't worry about exposing properties , Someone assigned it ( Avoid unreasonable operation of users ). Two things to note

The decorated method is called without parentheses

There are and can only be methods defined self One parameter

>>> class Goods(): def __init__(self,unit_price,weight): self.unit_price = unit_price self.weight = weight @property def price(self): return self.unit_price * self.weight>>> lemons = Goods(7,4)>>>>>> lemons.price28

The above method directly calls to... By calling attributes price Method ,property Encapsulate the complex process into the method , Call the corresponding method name when getting the value .

2.property Two methods of attribute definition

A、 Decorator mode

Apply to the methods of a class @property Decorator , That is, the way above .

B、 Class attribute mode

Create an instance object to assign values to class properties

>>> class Lemons(): def __init__(self,unit_price=7): self.unit_price = unit_price def get_unit_price(self): return self.unit_price def set_unit_price(self,new_unit_price): self.unit_price = new_unit_price def del_unit_price(self): del self.unit_price x = property(get_unit_price, set_unit_price, del_unit_price)>>> fruit = Lemons()>>> >>> fruit.x # call fruit.x Trigger get_unit_price7>>> >>> fruit.x = 9 # call fruit.x = 9 Trigger set_unit_price>>> >>> fruit.x9>>> >>> fruit.unit_price # call fruit.unit_price Trigger get_unit_price9>>> del fruit.x # call del fruit.x Trigger del_unit_price >>> >>> fruit.unit_priceTraceback (most recent call last): File "<pyshell#23>", line 1, in <module> l.unit_priceAttributeError: 'Lemons' object has no attribute 'unit_price'

property Method can accept four parameters

The first parameter is the name of the method that gets the property , call object . Property

The second parameter is the name of the method that sets the property , Automatically triggered when an attribute is assigned a value

The third parameter is the name of the method to delete the attribute , Automatically triggered when a property is deleted

The fourth parameter is a string , Is the description document of the attribute , Call object . attribute .doc Trigger when

3. use property Instead of getter and setter Method >>>class Watermelon(): def __init__(self,price): self._price = price # Private property , External cannot be modified and accessed def get_price(self): return self._price def set_price(self,new_price): if new_price > 0: self._price = new_price else: raise 'error: The price must be greater than zero '

use property Instead of getter and setter

>>>class Watermelon(): def __init__(self,price): self._price = price @property # Use @property decorate price Method def price(self): return self._price @price.setter # Use @property Decoration method , When the price assignment , Call decoration method def price(self,new_price): if new_price > 0: self._price = new_price else: raise 'error: The price must be greater than zero '>>> watermelon = Watermelon(4)>>> >>> watermelon.price4>>> >>> watermelon.price = 7>>> >>> watermelon.price7

This is about python @property This is where the article introduces the usage of , More about python @property Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you will support the software development network in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved