Home Backend Development Python Tutorial Python中使用动态变量名的方法

Python中使用动态变量名的方法

Jun 06, 2016 am 11:30 AM
python

如果要写一个程序,让x1为1,x2为2,然后直到x100为100,你会怎么做?

在C这种静态语言里,变量名这个标识符实际上会被编译器直接翻译成内存地址,所以除了手动设置每个变量的值以外,没办法做到这点。而Python这种动态语言则是可以做到的。

最容易想到的自然是eval,但是实际上根本不需要这种危险的东西,因为Python的变量名就是一个字典的key而已。要获取这个字典,直接用locals和globals函数即可。

因此这个程序可以这样实现:

代码如下:


>>> names = locals()
>>> for i in xrange(1, 101):
...   names['x%s' % i] = i
...
>>> x1
1
>>> x2
2
>>> x100
100

不过你也许会说这个例子没什么用,毕竟用数组来实现更为实用。

那么再考虑一个例子:服务器使用一种对象数据库,可以直接保存对象到数据库中。服务器列出目前支持的所有类,而用户想添加一个不存在于列表中的类,于是向服务器发送一段JSON或XML文本。服务器解析这段文本,将它转换成一个class对象,并且设置类名。之后用户就可以随意生成这个类的对象。
关键是这个数据库和类名相关,你不能用一个通用的Object类来保存所有的对象,否则查询时就乱套了。
而恰巧的是,还就有人在GAE论坛上提出了这个需求,而只会Java的他最终只能放弃。

当然,你想用来恶搞也行:

代码如下:


>>> locals()['True'] = False
>>> True
False

另一个用处就是测试一个变量名是否已经存在。标准的做法是try...except一个NameError异常,实际上直接用in locals()或in globals()就能判断了。
顺便再介绍另一种奇怪的方法,不知道有人这样写过没:

代码如下:


>>> import __main__
>>> hasattr(__main__, 'x')
False
>>> setattr(__main__, 'x', 1)
>>> x
1
>>> hasattr(__main__, 'x')
True

当然,没有任何人推荐你这样写,我也不会。

最后,除了动态设置变量名,动态删除也是可以的,例如del locals()['x1']。同样,delattr也是可用的。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles