0

I want to wrap a function that dynamically executes a file as code on Python 3. exec has optional arguments globals, locals, whose default values are globals() and locals() of the current context, respectively. How can I achieve this effect with my own wrapped functions?

  1. have tried def file(filepath,globals=globals(),locals=locals()) with no effect.
  2. Checking the implementation of exec, I found it defined in def exec(__object: Union[str, bytes, CodeType], __globals: Optional[Dict[str, Any]] = ... , __locals: Optional[Mapping[str, Any]] = ...) -> Any: ... , using this argument list directly will result in a syntax error.

The current function is defined like this

def file(filepath,globals,locals):
    with open(filepath, 'r', encoding='utf8') as textFileObj:
        return exec(compile(textFileObj.read(), filepath, 'exec'),globals,locals)

only be passed in manually in this way, and although this process can be simplified by methods such as snippet to achieve similar experience, it is always more convenient to have the default values.

Exe.file(Path.here(__file__,'HelloWorld.py'),globals(),locals())
Andy
  • 1,077
  • 1
  • 8
  • 20
  • 1
    What do you mean by "dynamically executes a file as code"? Can you explain how this is any different than simply importing the file? – wim Mar 23 '21 at 00:31
  • 1
    What does "the current context" mean? Please show a complete example of how you want to use the `file` function and what should happen as a result. But more importantly, **why are you messing around with all this stuff**? If the idea is just to import a module dynamically then you should ask about that - there are much better ways to do it. – Karl Knechtel Mar 23 '21 at 00:32
  • 1
    why are u doing this? u should import the function from `HelloWorld` and use it. – marcos Mar 23 '21 at 00:32
  • 1
    It's not clear to me why you would *want* the code in some other Python file to use your "current" set of globals or locals. Why would the other file have been written in a way that makes that make sense to do? – Karl Knechtel Mar 23 '21 at 00:33
  • Can you please check this https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument It explains why your approach with default values will not work. The default values are captured once and for all when the function is defined, so the file will run with the same context all the time unless you pass an explicit context. – Daniel Junglas Mar 23 '21 at 06:03
  • Several friends focused on examples. I understand what they said, but that's just an example, not the point of the question. Daniel junglas, the information seems to be very helpful. I'm checking it. Thank you. – Andy Mar 23 '21 at 07:39
  • Now I feel that this function is not provided for a good reason. If the default value is set to an expression that is almost certain to exist, for example, it is composed of some simple operators and built-in functions, then the problem is not big. But if someone wants to set something that is likely not to exist, then the uncertainty of this function is too big. – Andy Mar 23 '21 at 07:42
  • @DanielJunglas I have a question, why not present the information in the form of answers? If so, the answer can be adopted. – Andy Mar 23 '21 at 08:03
  • @Andy your question is now marked as duplicate and no longer accepts answers :-) In the beginning I only added this as a comment because I was not sure it would answer your question. – Daniel Junglas Mar 23 '21 at 08:38
  • Oh, I understand it. – Andy Mar 23 '21 at 08:51

0 Answers0