Evaluate the eval function in Python

Source: Internet
Author: User

Programmers disagree with the built-in functions of Python eval (). I found this problem when using the eval function:

>>> Class FOO: <br/>... val = 1 <br/>... </P> <p> Foo = Foo () <br/> Foo. val <br/> 1 <br/> eval_foo = eval ("foo") <br/> eval_foo <br/> <__main __. foo instance at 0x01c48080> <br/> eval_foo.val = 10000 <br/> Foo. val <br/> 10000 <br/> 

Instead of class, the value is passed.

>>> A = 1 <br/> B = eval ("") <br/> B <br/> 1 <br/> B = 10000 <br/> B <br/> 10000 <br/> >>> A <br/> 1 <br/> 

Think that int long float STR is a static (fixed length) type. When Python assigns values to these variables, it also allocates a definite space in the memory. After the value is re-assigned, when B = 10000 above, B has actually become another int object ..

However, if a class is used, it is only equivalent to obtaining a reference. Therefore, foo eval_foo points to the same object. Of course, the attributes are the same, and changing one will change the other.

In fact, it was later found that this feature was not related to eval ,,

 

If you do not want to obtain a reference, we need to use the copy function, including the copy function of the copy module and the deep copy function of the copy module ).

 

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.