#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' a test module '
__author__ = 'Zhang Shuai'
class A():
c = 1
a = A()
#hasattr
hasattr(a, "c") #True
hasattr(a, "d") #False
#getattr
getattr(a, "c") #1
getattr(a, "d") #AttributeError: 'A' object has no attribute 'd'
getattr(a, "d", 0) #0
#setattr
setattr(a, "d", 4)
getattr(a, "d") #4
setattr(a, "c", 4)
getattr(a, "c") #4
#查找若不存在则设置为3
getattr(a , "f", setattr(a, "f", 3))
#删除属性
delattr(object, name) #delattr(x, 'foobar')=del x.foobar
python hasattr(),getattr(),setattr()
最新推荐文章于 2021-02-03 12:07:56 发布