Using dynamic variable names in Python

Source: Internet
Author: User
This article describes how to use dynamic variable names in Python. if you want to write a program, set x1 to 1 and x2 to 2, then, what do you do until x100 is 100?

In a static language such as C, the variable name identifier is actually translated into a memory address by the compiler. Therefore, you cannot set the value of each variable manually. Python is a dynamic language.

The easiest thing to think of is eval, but it doesn't actually need such a dangerous thing, because the Python variable name is just a dictionary key. To obtain this dictionary, use the locals and globals functions.

Therefore, this program can be implemented as follows:

The code is as follows:


>>> Names = locals ()
>>> For I in xrange (1,101 ):
... Names ['X % s' % I] = I
...
>>> X1
1
>>> X2
2
>>> X100
100

However, you may say that this example is useless. after all, it is more practical to use arrays.

Another example is as follows: the server uses an object database to directly store objects in the database. The server lists all currently supported classes. if you want to add a class that does not exist in the list, you can send a JSON or XML text to the server. The server parses this text, converts it to a class object, and sets the class name. Then the user can generate the objects of this class at will.
The key is that the database is related to the class name. you cannot use a common Object class to store all objects; otherwise, the query will be messy.
Coincidentally, some people raised this demand on the GAE forum, but he could only give up on Java.

Of course, you can also use it to spoof:

The code is as follows:


>>> Locals () ['true'] = False
>>> True
False

Another use is to test whether a variable name already exists. The standard practice is to try... handle T a NameError exception. in fact, it can be determined directly by using in locals () or in globals.
By the way, I will introduce another strange method. I don't know if someone has written it like this:

The code is as follows:


>>> Import _ main __
>>> Hasattr (_ main __, 'X ')
False
>>> Setattr (_ main __, 'X', 1)
>>> X
1
>>> Hasattr (_ main __, 'X ')
True

Of course, no one recommends you to write like this, and I won't.

Finally, in addition to dynamically setting the variable name, dynamic deletion is also possible, such as del locals () ['X1']. Similarly, delattr is also available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.