博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python setattr()
阅读量:2555 次
发布时间:2019-05-11

本文共 2369 字,大约阅读时间需要 7 分钟。

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 function.

该函数与函数相对应。

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:    passd = Data()d.id = 10print(d.id)setattr(d, 'id', 20)print(d.id)

Output:

输出:

1020

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:nameEnter the attribute value:PankajData attribute = name and its value = Pankaj

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

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

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

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 
setattr(p, 'name', 'Pankaj')AttributeError: can't set attribute
. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://bxmzd.baihongyu.com/

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>