Methods for using dynamic variable names in Python _python

Source: Internet
Author: User
Tags in python

What would you do if you were to write a program that would allow X1 to be 1,x2 2 and then until X100 was 100?

In the static language of C, the identifier of the variable name is actually translated directly into the memory address by the compiler, so there is no way to do this except by manually setting the value of each variable. And Python is a dynamic language that can be done.

The easiest thing to think about is eval, but it doesn't really need this kind of dangerous stuff, because Python's variable name is just a dictionary key. To get this dictionary, use the locals and globals functions directly.

So this program can be implemented like this:

Copy Code code as follows:

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

But you might say this example is not useful, after all, using an array to achieve more practical.

Then consider an example: The server uses an object database that can directly save objects to the database. The server lists all the classes currently supported, and the user wants to add a class that does not exist in the list, and then sends a section of JSON or XML text to the server. The server parses the text, converts it to a class object, and sets the class name. Users can then randomly generate objects of this class.
The key is that the database is related to the class name, and you can't save all objects with a generic object class, otherwise the query will be messed up.
And coincidentally, someone in the Gae forum on this demand, and only Java he eventually can only give up.

Of course, you want to use it for a spoof:

Copy Code code as follows:

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

Another use is to test whether a variable name already exists. The standard procedure is to try...except a Nameerror exception, which can be judged directly in locals () or in Globals ().
By the way, another strange method is not known to anyone:

Copy Code code as follows:

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

Of course, no one recommended you to write like this, neither do I.

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

Related Article

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.