简体   繁体   English

Python:如何获取在另一个模块中定义的所有局部变量

[英]Python: How to get all of the local variables defined in another module

Is there a way to get a reference to the local variables defined in a different module? 有没有办法引用在不同模块中定义的局部变量?

for example, I have two files: framework.py and user_code.py: 例如,我有两个文件:framework.py和user_code.py:

framework.py: framework.py:

from kivy.app import App

class BASE_A:
    pass

class MyApp(App):
    def on_start(self):
        '''Here I'd like to get a reference to sub-classes of BASE_A and
        instantiated objects of these sub-classes, defined in the file
        "user_code.py" such as a1, a2, as well as the class A itself,
        without explicitly passing them to MyApp's instance.
        '''

user_code.py: user_code.py:

from framework import MyApp

class A(BASE_A):
    pass

app = MyApp()
a1 = A()
a2 = A()

app.run()

What I'd like to do is to somehow get a reference to the objects a1 and a2 , as well as the class A , that were all defined in user_code.py . 我想做的就是以某种方式获得对对象a1a2以及类A的引用,这些对象都在user_code.py中定义。 I'd like to use them in the method on_start , which is invoked in app.run() . 我想在on_start方法中使用它们,该方法在app.run()中调用。

Is it possible, for example, to get a reference to the scope in which the MyApp object was defined (user_code.py)? 例如,是否有可能获得对MyApp对象定义范围的引用(user_code.py)?

Some background for anyone who's interested: 有兴趣的人的一些背景知识:

I know it's a bit of an odd question, but the reason is: 我知道这是一个奇怪的问题,但原因是:

I'm writing a python framework for creating custom-made GUI control programs for self-made instruments, based on Arduino. 我正在编写一个Python框架,用于基于Arduino创建用于自制乐器的定制GUI控制程序。 It's called Instrumentino (sitting in GitHub ) and I'm currently developing version 2. 它称为Instrumentino (位于GitHub上 ),我目前正在开发版本2。

For people to use the framework, they need to define a system description file ( user_code.py in the example) where they declare what parts they're using in their system (python objects), as well as what type of actions the system should perform (python classes). 为了使人们能够使用该框架,他们需要定义一个系统描述文件(在示例中为user_code.py ),在其中声明他们在系统中使用的是什么部分(python对象)以及系统应采取的动作类型执行(python类)。

What I'm trying to achieve is to automatically identify these objects and classes in MyApp's on_start without asking the user to explicitly pass these objects and classes, in order to make the user code cleaner. 我想要实现的目的是在MyApp的on_start中自动识别这些对象和类,而不要求用户显式传递这些对象和类,以使用户代码更整洁。 Meaning to avoid code such as: 避免使用诸如以下代码的含义:

app.add_object(a1)
app.add_object(a2)
app.add_class(A)

New-style classes in Python have a method named __subclasses__ which returns a list of all direct subclasses that have been defined so far. Python中的新型类具有名为__subclasses__的方法,该方法返回到目前为止已定义的所有直接子类的列表。 You can use that to get a hold of the A class in your example, just call BASE_A.__subclasses__() (if you're using Python 2, you'll also need to change BASE_A to inherit from object ). 您可以使用它来获取示例中的A类,只需调用BASE_A.__subclasses__() (如果您使用的是Python 2,则还需要更改BASE_A以从object继承)。 See this question and its answers for more details (especially the functions to recursively get all subclasses). 有关更多详细信息,请参见此问题及其答案 (尤其是递归获取所有子类的函数)。

As for getting access to the instances, for that you probably should add some code to the base class, perhaps saving the instances created by __new__ into some kind of data structure (eg a weakset ). 至于访问实例,为此,您可能应该向基类中添加一些代码,也许将__new__创建的实例__new__到某种数据结构中(例如, weakset )。 See this question and its answers for more on that part. 有关部分的更多信息,请参见此问题及其答案 Actually, now that I think about it, if you put your instances into a centralized data structure somewhere (eg not in an attribute of each subclass), you might not need the function to search for the classes, since you can just inspect the type of the instances and find the subclasses that are being used. 实际上,考虑到这一点,如果您将实例放入某个位置的集中式数据结构中(例如,不在每个子类的属性中),则可能不需要该函数来搜索类,因为您可以检查type实例并找到正在使用的子类。

Your question is a bit illogical. 您的问题有点不合逻辑。

Since Python interprets the code sequentially: b is not defined before a initialization . 由于Python会顺序解释代码: ba initialization之前未定义。

If you can set b before a then: 如果你可以设置ba则:

b = None # global variable

class A():
    global b
    def __init__(self):
        '''Here I'd like to get a reference to b (of type B) without passing it as an argument'''

class B_BASE():
    pass

class B(B_BASE):
    def __init__(self):
        pass

if __name__ == '__main__':
    b = B()
    a = A()

I wouldn't recommend doing this because I find that this isn't clean. 我不建议这样做,因为我发现这不干净。 Since you have a dependency on b in a you should pass it as a parameter to the A class 由于您对ba b有依赖性,因此应将其作为参数传递给A class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM