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

Python uses the property function to create management method properties for classes

編輯:Python

1、 How to create manageable object properties ?

         Actual case :

                 In object oriented programming , We put the method into practice ( function ) As an interface to an object , Direct access to the properties of an object can be insecure , Or not flexible enough in design . But using calling methods is not as concise as accessing properties in form .

# Create a class of circles , Get the radius of the circle or set the radius of the circle , You need to add the following two methods to this class
# Accessor and setter forms :
circle.getRadius()
circle.setRadius(5.0) # Numerous
# Direct access form :
circle.radius
circle.radius = 5.0 # Jane 

                 Whether it is attribute access in form , But actually calling methods ?

         Solution :

                 Use property Function to create manageable properties for a class ( Method ),fget/fset/fdel Access corresponding attributes .

2、 Code demonstration

from math import pi
class Circle(object):
def __init__(self, radius):
# Circle radius
self.radius = radius
def get_radius(self):
# Reserved for radius 2 Decimal places are rounded off
return round(self.radius, 2)
def set_radius(self, value):
if not isinstance(value, (int, float)):
raise ValueError('wrong type')
self.radius = float(value)
def get_area(self):
# Obtain the area of the circle
return self.radius ** 2 * pi
# 3 Each parameter corresponds to , Access method 、 Setup method 、 Delete method
R = property(get_radius, set_radius)
c = Circle(3.2)
'''
Security :
Direct access to properties can be problematic , At some time, the user parameter value may be wrong ,
Passed a string , It is obviously wrong in logic that the radius cannot be a string ,
But there won't be any errors in the program . Because the properties of an instance can be of any type .
The user does not know that there is a logical error , Continue to do the calculation and the result shows that it is wrong .
'''
c.radius = 'abc'
d = c.radius * 2
print(d)
'''
Logically wrong but operationally correct , This leads to some inexplicable problems in the program .
I hope to know that the program has made mistakes at this point of assignment , This is the advantage of using method ,
When using methods, there are more means , You can type check parameter values .
'''
# c.set_radius('abc')
c2 = Circle(3.2)
'''
flexibility :
At the beginning, the radius of the circle always returns whatever the user outputs ,
Later, the requirements changed, and the radius that the user wants to see is the original input
Keep two decimal places and round the result .
In this case, if the method is used, it is easy to solve the problem ,
This involves a hidden , Don't want users to see real things .
'''
# User radius It hasn't changed but the results have changed
print(c2.get_radius())
# Use property The function calls each method safely, concisely and efficiently
c3 = Circle(3.2)
# Would call property The first 1 Access methods get_area
print(c3.R)
# Assignment time , Would call property The first 3 Settings set_radius
c3.R = 5.9
print(c3.R)


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