Setting the function Environment--setfenv

Source: Internet
Author: User

When we define variables in the global environment, there are often naming conflicts, especially when using some libraries, variable declarations can overwrite, and a non-global environment is needed to solve this problem. The SETFENV function can meet our needs.

  Setfenv (f, table): Setting the environment for a function

(1) When the first argument is a function, the environment that sets the function

(2) When the first argument is a number, 1 represents the current function, 2 represents the function that calls itself, 3 represents the function that calls its own function, and so on

The so-called function of the environment, in fact, an environment is a table, the function is limited to access only the fields in the table, or in the function body of its own defined variables. In the following example, setting the current function's environment as an empty table, the print function from the global will not be visible after the set execution, so the call will fail.

--an environment is a table that records all the domains that the new environment can access newfenv = {}setfenv (1, newfenv) print (1)        --Attempt to call global ' print ' (a nil value)

We can inherit existing fields like this:

A = 10newfenv = {_g = _g}setfenv (1, newfenv) _g.print (1)-        -1_g.print (_G.A)--        10_g.print (a)        --nil note here is nil, new Environment does not have a domain, but can access _g's a domain through _G.A

As you can see, the _g is accessible in the new environment, but one thing is that all functions in the _g must be called manually, which is inconvenient. We can use metatable to make improvements to the above code:

--any assignment operates on a new table, without worrying about modifying the global variable table with the wrong operation. In addition, you can still modify global variables by _g: newfenv = {}setmetatable (newfenv, {__index = _g}) setfenv (1, newfenv) print (1)        --1 The new environment directly inherits all domains of the global environment, with the benefit that it is not necessary to manually invoke the _g through the

This way, when you access a variable that does not exist in the function, it is automatically found in _g. Variables that exist for both the current function and the _g can be distinguished by whether the _g display call is used, for example, if there are two A, then _g.a represents the inheritance, and a is the current function environment.

In addition, the getfenv (f) function can be used to view the environment in which the function is located, and the global environment _g is returned by default.

Http://www.cnblogs.com/sifenkesi/p/3843348.html

Set function Environment--setfenv (GO)

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.