当前位置:网站首页>Use of getattr, hasattr, delattr and setattr in reflectors
Use of getattr, hasattr, delattr and setattr in reflectors
2022-07-27 06:39:00 【Leoon123】
Reflection
Mapping strings to instance variables or instance methods ,
Then you can call 、 Modify the operating
There are four important ways to reflect :
- 1、getattr Get object properties / Method
- 2、hasattr Determine whether the object has corresponding attributes
- 3、delattr Delete the specified attribute
- 4、setattr Set content for object
getattr Get object properties / Method
class M211Vip:
def welcome(self):
print(" Congratulations on joining the organization !!!!!")
def learning(self):
print(" Is opening up the gap with others ......")
No use getattr Call before
M211Vip().mylearning()
Use getattr call mylearning() Method , The difference is mylearning Is string
getattr(M211Vip(), "mylearning")()
setattr Set content for object
mylearning Is an external method of a class , stay M211Vip Cannot call , So you need to map the functions outside the class to the methods inside the class
class M211Vip:
def welcome(self):
print(" Congratulations on joining the organization !!!!!")
def learning(self):
print(" Is opening up the gap with others ......")
def mylearning():
print(" In self-study ....")
Functions outside the class map to methods inside the class
bo = M211Vip()
setattr(bo, "mylearning", mylearning) # Map first , Can be used getattr(bo, "mylearning")() Method
Use ordinary method calls , Or use getattr You can call this method
bo.mylearning()
getattr(bo, "mylearning")()

hasattr Determine whether the object has corresponding attributes
class M211Vip1:
def __init__(self, name, wkage):
self.name = name
self.wkage = wkage
def welcome(self):
print(" Congratulations on joining the organization !!!!!")
def learning(self):
print(" Is opening up the gap with others ......")
bo = M211Vip1(" wave ", "3")
name = getattr(bo, "name") # Map the string to the instance variable
if hasattr(bo, "name"):
print(" Yes name attribute ....")
else:
print(" no name attribute ....")

delattr Delete the specified attribute
# 4、delattr Delete the specified attribute
delattr(bo, "name")
# print(bo.wkage)
if hasattr(bo, "name"):
print(" Yes name attribute ....")
else:
print(" no name attribute ....")

边栏推荐
- Interpretation of unity desktop version 7.6
- 1. CentOS 7 安装 redis
- Iptables firewall, SNAT and DNAT
- rsync远程同步
- Li Kou daily question leetcode 513. find the value in the lower left corner of the tree
- Kaggle calls the custom module method
- PXE efficient batch network installation
- Vscode solves the problem of using stuck ipynb files when running
- Shell语句判断练习题
- 数组及下标索引
猜你喜欢

数据库的约束以及设计

If conditional statement of shell

Detailed explanation of thread safety problems

Shell -- conditional statements (if statements, case statements)

shell--变量的运算

Stress performance test of website based on AB tool under Apache

ROS topic name setting

关于testng相关标签的使用

哈希表简介

Knowledge supplement of multithreading
随机推荐
The concept of interface testing and the use of postman tools
ROS distributed communication
TF coordinate transformation
Programming learning records - Lesson 7 [functions]
Shell -- custom variables and assignments
Path to file
互联网简单协议概括
Advanced ROS communication mechanism
如何规范式编写yaml文件
selenium知识点
Binary tree - search tree
Programming learning records - Lesson 9 [operators]
接口测试流程及面试问题
C language - file operation
Shell语句判断练习题
Shell sentence judgment exercise
Array and subscript index
Basic knowledge of English: modifying sentence elements - Part 1
shell之if条件语句
bug分类及缺陷和csv文件测试