创建一个“即时”变量“ [英] Create a variable "on the fly"
问题描述
Python可以动态创建变量吗?例如,我想
类似......
make_variable(''OSCAR'',''the grouch'');
打印OSCAR;
....输出...
the grouch
Python中有这样的东西吗?
如果有人有兴趣,我想基于环境变量实例化一组变量
使用os.environ到处都是使用
a来查看相应类型的Python变量。
谢谢,
Paul DS。
-
请删除x-如果回复发件人。
Can Python create a variable "on-the-fly". For example I would like
something like...
make_variable(''OSCAR'', ''the grouch'');
print OSCAR;
....to output...
the grouch
Anything like this in Python?
And in case anyone is interested, I want to instantiate a set of variables
based on environment variables without using os.environ everywhere by having
a look instantiate Python variables of the appropriate type.
Thanks,
Paul DS.
--
Please remove the "x-" if replying to sender.
推荐答案
Paul D.Smith写道:
Paul D.Smith wrote:
Python可以创建一个变量on-the -fly" ;.例如,我想
类似......
make_variable(''OSCAR'',''grouch'');
打印OSCAR;
...输出...
Can Python create a variable "on-the-fly". For example I would like
something like...
make_variable(''OSCAR'', ''the grouch'');
print OSCAR;
...to output...
Python只有''动态''变量和'';''不用于一个
表达式在一行。
也许教程也很好读。
Paolino
___________________________________
Yahoo!邮件:免费1GB每日消息10MB
http://mail.yahoo.it
> make_variable(''OSCAR'',''grouch'');
> make_variable(''OSCAR'', ''the grouch'');
print OSCAR;
print OSCAR;
尝试使用setattr。 (它在__builtins__中;你不必输入
任何东西。)
Try using "setattr". (It''s in __builtins__; you don''t have to import
anything.)
print setattr .__ doc __
print setattr.__doc__
setattr(对象,名称,值)
Set对象的命名属性; setattr(x,''y'',v)相当于
``xy = v''''。
-
如果建筑商按照程序员编写程序的方式建造建筑物,那么第一只啄木鸟会摧毁文明。
- 原作者未知
setattr(object, name, value)
Set a named attribute on an object; setattr(x, ''y'', v) is equivalent to
``x.y = v''''.
--
If builders built buildings the way programmers write programs,
the first woodpecker that came along would destroy civilization.
- Original author unknown
Paul D.Smith写道:
Paul D.Smith wrote:
Python可以在运行中创建一个变量。例如,我想
类似......
make_variable(''OSCAR'',''grouch'');
打印OSCAR;
...输出...
Can Python create a variable "on-the-fly". For example I would like
something like...
make_variable(''OSCAR'', ''the grouch'');
print OSCAR;
...to output...
Python只有''动态''变量和'';''不用于一个
表达式在一行中。
也许教程也很好读。
Paolino
Python has only ''on the fly'' variables and '';'' is not used for one
expression in one line.
Probably the tutorial is good to be read also.
Paolino
>
这篇关于创建一个“即时”变量“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!