在其他模块的上下文中使用类 [英] Use a class in the context of a different module

查看:98
本文介绍了在其他模块的上下文中使用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改标准库中的某些类,以使用该模块中其他类所使用的一组不同的全局变量.

I want to modify some classes in the standard library to use a different set of globals the ones that other classes in that module use.

此示例仅是一个示例:

# module_a.py

my_global = []

class A:
    def __init__(self):
        my_global.append(self)

class B:
    def __init__(self):
        my_global.append(self)

在此示例中,如果我通过A()创建A的实例,它将在my_global命名的对象上调用append.但是现在我希望创建一个新模块,将B导入到其中,并让B使用从其导入到的模块中的my_global,而不是最初定义的来自模块Bmy_global.

In this example, If I create an instance of A, via A(), it will call append on the object named by my_global. But now I wish to create a new module, import B to it, and have B use my_global from the module it's been imported into, instead of the my_global from the module B was original defined.

# module_b.py

from module_a import B

my_global = []

相关

我正在努力解释自己的问题,这是我以前的尝试,实际上确实提出了完全不同的问题:

Related

I'm struggling to explain my problem, here is my previous attempt which did in fact ask something completely different:

  • 以上示例仅用于说明我要实现的目标.
  • 由于类没有可变范围(与C ++不同),我认为对globals映射的引用未存储在类中,而是在定义时附加到每个函数.

标准库要求提供一个示例:

An example was requested from the standard library:

模块使用诸如_allocate_lockget_ident_active之类的全局变量,它们被定义为

Many (maybe all?) of the classes in the threading module make use of globals such as _allocate_lock, get_ident, and _active, defined here and here. One cannot change these globals without changing it for all the classes in that module.

推荐答案

您不能在不影响模块的所有其他用户的情况下更改全局变量,但是您可以 做的是创建一个整个模块的私有副本.

You can't change the globals without affecting all other users of the module, but what you sort of can do is create a private copy of the whole module.

我相信您熟悉 sys.modules ,并且如果您删除模块从那里开始,Python忘记了它的导入,但是引用它的旧对象将继续这样做.再次导入后,将创建该模块的新副本.

I trust you are familiar with sys.modules, and that if you remove a module from there, Python forgets it was imported, but old objects referencing it will continue to do so. When imported again, a new copy of the module will be made.

针对您的问题的骇人听闻的解决方案可能是这样的:

A hacky solution to your problem could would be something like this:

import sys
import threading

# Remove the original module, but keep it around
main_threading = sys.modules.pop('threading')

# Get a private copy of the module
import threading as private_threading

# Cover up evidence by restoring the original
sys.modules['threading'] = main_threading

# Modify the private copy
private_threading._allocate_lock = my_allocate_lock()

现在,private_threading.Lock的全局变量与threading.Lock完全分开!

And now, private_threading.Lock has globals entirely separate from threading.Lock!

不用说,并不是在编写模块时就考虑了这一点,尤其是在使用诸如threading之类的系统模块时,您可能会遇到问题.例如,threading._active应该包含所有正在运行的线程,但是使用此解决方案,_active都不会全部拥有它们.该代码还可能会吞噬您的袜子,并使您的房屋着火等.严格测试.

Needless to say, the module wasn't written with this in mind, and especially with a system module such as threading you might run into problems. For example, threading._active is supposed to contain all running threads, but with this solution, neither _active will have them all. The code may also eat your socks and set your house on fire, etc. Test rigorously.

这篇关于在其他模块的上下文中使用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆