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 .
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)
List of articles 1. send out o
Take the following picture as
Catalog One 、 Draw a scatter