Python3.x basic learning-@property

@property


1 @property has a built-in decorator function that turns a method call method into a property call method. (Use a method as a property)
Note that @property decorator can only be used in object-oriented
2 Access Use @property decorator to decorate functions can directly call the function name (will perform a function (function) and then return the value)
3. @ Property decorators can only modify methods without parameters

class Person:
    def __init__(self,name,age):
        self.name =name
        self.age = age

    @property
    def get_name(self):
        return self.name

    def get_age(self):
        return self.age

p1 =Person('johnson',22)
print(p1.get_age())
print(p1.get_name)

# 22
# johnson

 

# 1. Calculate the circumference of a circle and the area 
# circle defining the classes, attributes: Radius R & lt, PI: 3.14 
#            Method: find the circumference, the area of 

class Circle:
     DEF  the __init__ (Self, R & lt): 
        self.PI = 3.14 
        self.r = r 

    @property 
    def calcu_circle (self):
         return 2 * self.PI * self.r 

    @property 
    def calcu_area (self):
         return self.PI * self.r ** 2 

circle = Circle (25 )
 print (circle.calcu_circle , '  ' , circle.calcu_area) 

# 157.0 1962.5

 

Guess you like

Origin www.cnblogs.com/johnsonbug/p/12709956.html