Python setattr()

Python setattr() function allows us to set an object attribute value.

Python setattr()函数允许我们设置对象属性值。

Python setattr() (Python setattr())

Python setattr() function syntax is:

Python setattr()函数语法为:

setattr(object, name, value)

This function is counterpart of python getattr() function.

该函数与python getattr()函数相对应。

The input arguments are object whose attribute will be set, name is the attribute name and value is the attribute value.

输入参数是将设置属性的objectname是属性名称, value是属性值。

Let’s look at a simple example of setattr() function.

我们来看一个简单的setattr()函数示例。

class Data:
    pass


d = Data()

d.id = 10

print(d.id)

setattr(d, 'id', 20)
print(d.id)

Output:

输出:

10
20

So setattr() does the exact same thing as using dot operator with the object.

因此,setattr()的作用与对对象使用点运算符的作用完全相同。

So where is the benefit of using setattr() function?

那么使用setattr()函数的好处在哪里呢?

The setattr() function is useful in dynamic programming where the attribute name is not static. In that case, we can’t use the dot operator. For example, taking user input to set an object attribute and its value.

setattr()函数在属性名称不是静态的动态编程中很有用。 在这种情况下,我们不能使用点运算符。 例如,以用户输入来设置对象属性及其值。

用户输入的Python setattr()示例 (Python setattr() example with user input)

d = Data()
attr_name = input('Enter the attribute name:\n')
attr_value = input('Enter the attribute value:\n')

setattr(d, attr_name, attr_value)

print('Data attribute =', attr_name, 'and its value =', getattr(d, attr_name))

Output:

输出:

Enter the attribute name:
name
Enter the attribute value:
Pankaj
Data attribute = name and its value = Pankaj

Python setattr()异常 (Python setattr() exception)

We can create a read-only attribute in the object using property function or property decorator.

我们可以使用property functionproperty decorator在对象中创建一个只读属性。

In that case, if we try to set the attribute value using setattr() function, we will get AttributeError: can't set attribute exception.

在这种情况下,如果尝试使用setattr()函数设置属性值,则会得到AttributeError: can't set attribute异常。

class Person:

    def __init__(self):
        self._name = None

    def get_name(self):
        print('get_name called')
        return self._name

    # for read-only attribute
    name = property(get_name, None)


p = Person()

setattr(p, 'name', 'Pankaj')

Output:

输出:

Traceback (most recent call last):
  File "/Users/pankaj/Documents/github/journaldev/Python-3/basic_examples/python_setattr_example.py", line 39, in <module>
    setattr(p, 'name', 'Pankaj')
AttributeError: can't set attribute
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23129/python-setattr

  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值