- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
。
。
hasattr()函数用于判断是否包含对应的属性 。
语法:
hasattr(object,name) 。
参数:
object--对象 。
name--字符串,属性名 。
返回值:
如果对象有该属性返回True,否则返回False 。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class
People:
country
=
'China'
def
__init__(
self
,name):
self
.name
=
name
def
people_info(
self
):
print
(
'%s is xxx'
%
(
self
.name))
obj
=
People(
'aaa'
)
print
(
hasattr
(People,
'country'
))
#返回值:True
print
(
'country'
in
People.__dict__)
#返回值:True
print
(
hasattr
(obj,
'people_info'
))
#返回值:True
print
(People.__dict__)
##{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10205d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
|
。
。
描述:
getattr()函数用于返回一个对象属性值 。
语法:
getattr(object,name,default) 。
参数:
object--对象 。
name--字符串,对象属性 。
default--默认返回值,如果不提供该参数,在没有对于属性时,将触发AttributeError.
返回值:
返回对象属性值 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class
People:
country
=
'China'
def
__init__(
self
,name):
self
.name
=
name
def
people_info(
self
):
print
(
'%s is xxx'
%
(
self
.name))
obj
=
getattr
(People,
'country'
)
print
(obj)
#返回值China
#obj=getattr(People,'countryaaaaaa')
#print(obj)
#报错
# File "/getattr()函数.py", line 32, in <module>
# obj=getattr(People,'countryaaaaaa')
# AttributeError: type object 'People' has no attribute 'countryaaaaaa'
obj
=
getattr
(People,
'countryaaaaaa'
,
None
)
print
(obj)
#返回值None
|
。
。
描述:
setattr函数,用于设置属性值,该属性必须存在 。
语法:
setattr(object,name,value) 。
参数:
object--对象 。
name--字符串,对象属性 。
value--属性值 。
返回值:
无 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class
People:
country
=
'China'
def
__init__(
self
,name):
self
.name
=
name
def
people_info(
self
):
print
(
'%s is xxx'
%
(
self
.name))
obj
=
People(
'aaa'
)
setattr
(People,
'x'
,
111
)
#等同于People.x=111
print
(People.x)
#obj.age=18
setattr
(obj,
'age'
,
18
)
print
(obj.__dict__)
#{'name': 'aaa', 'age': 18}
print
(People.__dict__)
#{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1007d5620>, 'people_info': <function People.people_info at 0x10215d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, 'x': 111}
|
。
。
描述:
delattr函数用于删除属性 。
delattr(x,'foobar)相当于del x.foobar 。
语法:
setattr(object,name) 。
参数:
object--对象 。
name--必须是对象的属性 。
返回值:
无 。
示例:
1
2
3
4
5
6
7
8
9
|
class
People:
country
=
'China'
def
__init__(
self
,name):
self
.name
=
name
def
people_info(
self
):
print
(
'%s is xxx'
%
(
self
.name))
delattr
(People,
'country'
)
#等同于del People.country
print
(People.__dict__)
{
'__module__'
:
'__main__'
,
'__init__'
: <function People.__init__ at
0x1006d5620
>,
'people_info'
: <function People.people_info at
0x10073d1e0
>,
'__dict__'
: <attribute
'__dict__'
of
'People'
objects>,
'__weakref__'
: <attribute
'__weakref__'
of
'People'
objects>,
'__doc__'
:
None
}
|
补充示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class
Foo:
def
run(
self
):
while
True
:
cmd
=
input
(
'cmd>>: '
).strip()
if
hasattr
(
self
,cmd):
func
=
getattr
(
self
,cmd)
func()
def
download(
self
):
print
(
'download....'
)
def
upload(
self
):
print
(
'upload...'
)
# obj=Foo()
# obj.run()
|
总结 。
以上所述是小编给大家介绍的详解Python3 中hasattr()、getattr()、setattr()、delattr()函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:http://www.cnblogs.com/zzzhfo/p/8871661.html 。
最后此篇关于详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数的文章就讲到这里了,如果你想了解更多关于详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果我使用 setattr(p,'wrong_attr','value') 而不是 p=MyModel.objects.filter(WRONG_ATTRIBUTE=value) 我没有收到与不存在的
我有一个定义了多个方法的类。 import mat class Klass(object): @mat.sell(mat.CanSet): def method1(self):
我不知所措。 找不到任何其他有助于解决此问题的内容。 dta = {'type': "", 'content': ""} print dta >>>{'content': '', 'type': ''
这就是我的意思: >>> class Foo: pass >>> foo = Foo() >>> setattr(foo, "@%#$%", 10) >>> foo.@%#$% Syn
这个问题已经有答案了: What do lambda function closures capture? (8 个回答) 已关闭 3 年前。 我正在尝试创建一个 class适应django反对导出。
我想了解对象删除在 python 上的工作原理。这是一组非常简单的代码。 class A(object): def __init__(self): setattr(self,
我有一个类(bot),它有一个属性“health”;由于这个类有很多参数,并且我希望用户输入很多参数,所以我选择循环遍历 {param:explanation} 的字典,并为每个参数输入一个要设置的值
我应该将什么作为第一个参数“object”传递给函数 setattr(object, name, value) , 在当前模块上设置变量? 例如: setattr(object, "SOME_CONS
我试图制作一个可以一次性将多个属性分配给不同值的类。因此,我尝试修改 setattr 来执行此操作。 class hello(): def __setattr__(self,attr,valu
我想使用 setattr 更改列的因子水平. However, when the column is selected the standard data.table方式( dt[ , col] ),
为什么在附加或连接对象属性的列表或字典时未调用 __setattr__ 方法。方法描述“在尝试属性分配时调用。”会让我相信连接两个列表是可行的,但是 __setattr__ 方法仍然没有被调用。 cl
我想以编程方式更改类的某些属性。 我发现有setattr函数,但它没有按预期工作。 > obj.setattr('bar', 99) 'MyClass' object has no attribute
我正在创建一个 sigsum() 函数,它使用输入方程和输入变量求和。这是我到目前为止所拥有的: def sigsum(eqn, index, lower=0, upper=None, step=1)
我在 cython 中有一个 cdef 类,我想用 setattr 内置函数初始化它的字段。但是,当我这样做时出现执行错误: /path/.../cimul.cpython-34m.so in cim
我有一个模型类,它有一个引用 django DB 对象的属性。我想使用带有 setattr() 的单个 View 来更改此属性,我用它来更改此对象的任何属性。 问题是,我似乎无法通过堆栈传递对象实例。
我想使用 setattr 创建绘图: import numpy as np import matplotlib.pyplot as plt x = np.random.rand
我试图动态地向类添加方法,但我一直遇到错误,其中 self 未传递给新函数。例如: class Dummy(): def say_hi(self): print("hi") d
我正在编写的一个类需要使用存储 numpy 数组的变量名属性。我想为这些数组的切片赋值。我一直在使用 setattr,这样我就可以让属性名称有所不同。我为切片赋值的尝试是: class Dummy(o
我对 python 还很陌生,但是我需要与一些作为一堆 python 模块编写的软件进行交互(.py 文件,以防我错误地将它们识别为“模块”。)这个程序有一些非常有用的功能以及我确实无法破解的复杂功能
class Product(models.Model): name = models.CharField(max_length=50) desc = models.CharFiel
我是一名优秀的程序员,十分优秀!