getattr / setattr q。 [英] getattr/setattr q.

查看:75
本文介绍了getattr / setattr q。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


在C级,我可以做setattr(C,''x'',10)。


是否可以将getattr / setattr用于不在
类或类似内容中的变量?我的意思与

exec(" x = 10")相同。


谢谢。

解决方案

Paulo da Silva写道:


在C类中,我可以做setattr(C,''x'',10 )。


是否可以将getattr / setattr用于不在
类或类似内容中的变量?我的意思是与

exec(x = 10)相同的结果。



如果你在模块级别,你可以这样做:


globals()[''x ''] = 10


如果你在一个函数中,你可能想要寻找另一种方式

做你正在做的事情。


你想在这里完成的实际任务是什么?


STeVe


Paulo da Silva< ps ******** @ esotericaX.ptXwrites:


在C级,我可以做SETATTR(C '' X '',10)。



这将在C类上设置一个属性,由该类的所有

实例共享。


如果你想在一个实例上设置一个属性,你需要这样做

实例对象::


>> class Foo(object):



... def __init __(self):

... setattr(self,''bar'',10)

...


>> Foo.bar



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

AttributeError:type object' 'Foo''没有属性''bar''


>> spam = Foo()
spam.bar



10


是否可以将getattr / setattr用于不在
类或类似内容中的变量?我的意思是与

exec(x = 10)相同的结果。



不在类或函数内的变量是

模块的属性(所谓的全局属性)。因此,您可以在模块对象上使用setattr

::
$ blockquote class =post_quotes>


>> import sys


>> def foo():



... this_module = sys.modules [__ name__]

... setattr(this_module,''bar'',10)

...


>> bar



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

NameError:名称''bar''未定义


> > foo()
bar



10


-

\我开始认为生活只是一个长的小野洋子|

` \ album;没有押韵或理由,只有很多不连贯的尖叫声和|

_o__)然后它就结束了。 - Ian Wolff |

Ben Finney


4月2日晚上10点08分,Paulo da Silva< psdasil ... @ esotericaX .ptXwrote:


是否可以将getattr / setattr用于不在

类中的变量......?



python文档对

setattr()的定义有什么看法?


Hi!

In a class C, I may do setattr(C,''x'',10).

Is it possible to use getattr/setattr for variables not inside
classes or something equivalent? I mean with the same result as
exec("x=10").

Thanks.

解决方案

Paulo da Silva wrote:

In a class C, I may do setattr(C,''x'',10).

Is it possible to use getattr/setattr for variables not inside
classes or something equivalent? I mean with the same result as
exec("x=10").

If you''re at the module level, you can do::

globals()[''x''] = 10

If you''re inside a function, you probably want to look for another way
of doing what you''re doing.

What''s the actual task you''re trying to accomplish here?

STeVe


Paulo da Silva <ps********@esotericaX.ptXwrites:

In a class C, I may do setattr(C,''x'',10).

That would set an attribute on the class C, shared by all instances of
that class.

If you want to set an attribute on an instance, you need to do so on
the instance object::

>>class Foo(object):

... def __init__(self):
... setattr(self, ''bar'', 10)
...

>>Foo.bar

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object ''Foo'' has no attribute ''bar''

>>spam = Foo()
spam.bar

10

Is it possible to use getattr/setattr for variables not inside
classes or something equivalent? I mean with the same result as
exec("x=10").

"Variables not inside classes or functions" are attributes of the
module (so-called "global" attributes). Thus, you can use setattr on
the module object::

>>import sys

>>def foo():

... this_module = sys.modules[__name__]
... setattr(this_module, ''bar'', 10)
...

>>bar

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ''bar'' is not defined

>>foo()
bar

10

--
\ "I''m beginning to think that life is just one long Yoko Ono |
`\ album; no rhyme or reason, just a lot of incoherent shrieks and |
_o__) then it''s over." -- Ian Wolff |
Ben Finney


On Apr 2, 10:08 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

Is it possible to use getattr/setattr for variables not inside
classes...?

What does the python documentation say about the definition of
setattr()?


这篇关于getattr / setattr q。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆