The magical use of property & eval() in Python

Friends who have learned the Java programming language should all know that they will learn the getter & setter methods of class attributes during the learning process of the Java language. So is there a similar method in Python? The answer is yes.

Today's sharing content is: How to use the @property decorator to implement the getter & setter method in Python.

Before the actual code operation, let's first understand the little knowledge related to the @property decorator. Conducive to a better understanding of the following practical code.

1. Python has three built-in decorators:
@staticmethod (static method)
@classmethod (class method)
@property (descriptor)
where @property is a very useful syntactic sugar (syntactic sugar refers to those that have not added new to the computer language Function, but only a "sweet" grammar for humans. The reason why it is affectionately called "sweet" grammar is because syntactic sugar often provides programmers with more practical coding methods, which is beneficial to better coding Style, more readable).

The biggest advantage of @property is to turn a method into a property call in a class. It is a built-in function of Python. It is often used to decorate class methods and is used to call functions by accessing properties.

 

2. Python code implementation:

1class Person:
2    def __init__(self,name,age,salary):
3        self.__name=name
4        self.__age=age
5        self.__salary=salary
6
7    def set_name(self,name):
8        self.__name=name
9
10    def get_name(self):
11        return self.__name
12
13    def set_age(self,age):
14        self.__age=age
15
16    def get_age(self):
17        return self.__age
18
19    def set_salary(self,salary):
20        self.__salary=salary
21
22    def get_salary(self):
23        return self.__salary
24
25    @property
26    def age(self):
27        return self.__age
28
29    @age.setter
30    def age(self,age):
31        self.__age=age
32
33if __name__ == '__main__':
34    p1 = Person('tom',11,1000)
35    print(p1.get_age())
36    p1.set_age(100)
37    print(p1.get_age())
38
39    print(p1.age)
40    p1.age = 28
41    print(p1.age)
42
43    #eval()的功能:将字符串str当成有效的表达式来求值并返回计算结果。如下是将字符串转换成方法名再调用:
44    def man():
45        return "good job"
46    print(eval("man")())
47
48    #eval()可以把list, tuple, dict和string相互转化,这里以list为示例进行演示:
49    list1 = "[33,2,22,11,44,55]"
50    print(type(list1))
51    list2 = eval(list1)
52    print(type(list2))
53    print(list2)
54    print(list2[0])

 

3. Define read-only attributes in Python

    Defining read-only properties in Python is not @property, and defining read-only properties is also very simple: use the property that needs to be defined as the method name (for example, line 26: define the age attribute as a method), and add to the method Built-in decorator @property, while still using the property name as the method name (for example, line 30: define the age property as a method, and add @age.setter to the method name).

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/112101514