Home >Backend Development >Python Tutorial >Introduction to the usage of hasattr(), getattr(), and setattr() in Python (code example)

Introduction to the usage of hasattr(), getattr(), and setattr() in Python (code example)

不言
不言forward
2018-12-30 10:49:192146browse

This article brings you an introduction to the usage of hasattr(), getattr(), and setattr() in Python (code examples). It has certain reference value. Friends in need can refer to it. I hope It will help you.

1. hasattr(object, name)

Determine whether there is a name attribute in the object. If so, it will return True, if not, it will return False.

class MyClass(object):
    name = 'jack'
    age = '22'


obj = MyClass()
print(hasattr(obj, 'name'))  # True
print(hasattr(MyClass, 'age'))  # True
print(hasattr(obj, 'gender'))  # False

2. getattr(object, name, [default])

is used to get the attributes or methods of the object. If it is available, print it out. If it is not, print the default value. , if the default value is not set, an error will be reported

class MyClass(object):
    name = 'jack'
    age = '22'

    def func(self):
        return 'hello world!!!'


obj = MyClass()

print(getattr(MyClass, 'name'))  # jack
print(getattr(obj, 'age'))  # 22
print(getattr(MyClass, 'func'))  # <function MyClass.func at 0x000001ACDE9A9AE8>
print(getattr(obj, 'func'))  # <bound method MyClass.func of <__main__.MyClass object at 0x000001D1505D01D0>>
# print(getattr(MyClass, 'func1'))  # 没有设置默认值,找不到方法会报错: AttributeError: type object 'MyClass' has no attribute 'func1'
print(getattr(MyClass, 'func1', None))  # 设置了默认值None,找不到就会返回默认值: None

print(getattr(MyClass, 'func')('self'))  # hello world!!!
print(getattr(obj, 'func')())  # hello world!!!

3. setattr(object, key, value)

is used to assign value to the attribute key of the object. If the key exists , then update the value of value. If the key does not exist, create the attribute key first and then assign value to it.

class MyClass(object):
    name = 'jack'
    age = '22'


obj = MyClass()

setattr(MyClass, 'name', 'tom')
print(getattr(MyClass, 'name'))  # tom

setattr(obj, 'age', 28)
print(getattr(obj, 'age'))  # 28

setattr(MyClass, 'gender', 'male')
print(getattr(MyClass, 'gender'))  # male
print(getattr(obj, 'gender'))  # male

setattr(obj, 'hobby', 'skating')
print(obj, 'hobby')  # <__main__.MyClass object at 0x00000209F5070630> hobby
print(MyClass, 'hobby')  # <class &#39;__main__.MyClass&#39;> hobby

Use three methods together:

class MyClass(object):
    name = 'jack'
    age = '22'


# 判断Myclass是否有gender属性,有则打印,没有则添加
def if_attr(gender='male'):
    if hasattr(MyClass, 'gender'):
        return getattr(MyClass, 'gender')
    return setattr(MyClass, 'gender', gender)


if_attr(gender='female')
print(getattr(MyClass, 'gender'))  # female

The above is the detailed content of Introduction to the usage of hasattr(), getattr(), and setattr() in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete