开发者

Python module get top scope variables

开发者 https://www.devze.com 2023-02-09 15:13 出处:网络
Hey, in my python modules (written in python) I want to be able to access the variables from the top level scope, that is, the file that开发者_开发问答 is calling the modules. How would I go about doi

Hey, in my python modules (written in python) I want to be able to access the variables from the top level scope, that is, the file that开发者_开发问答 is calling the modules. How would I go about doing this? Thanks.


There's no way that will work in all environments, so a precise answer might depend on how you are running your top-level Python code. The best thing to do is to put the variables into an object and pass the object to the functions that need it.


In general, the way to do this is to provide some API to your plugins (e.g. a module that they import) to provide controlled access to the information they need.

If this information may vary by context, and passing in different arguments to a plugin initialisation function isn't appropriate, then an information API that uses threading.local under the covers may be of value.

0

精彩评论

暂无评论...
验证码 换一张
取 消