Python - 我可以访问调用我的对象吗?

发布于 2024-08-24 18:20:00 字数 616 浏览 12 评论 0原文

如果我有这样的想法:

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

class C:
    def otherFunction(self):
        # here I wan't to have acces to the instance of A or B who call me.

...

# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B

尽管存在设计或其他问题,但这只是一个好奇心的问题。

注意:这必须在不更改的情况下完成 otherFunction 签名

If I have this:

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

class C:
    def otherFunction(self):
        # here I wan't to have acces to the instance of A or B who call me.

...

# in main or other object (not matter where)
a = A()
b = B()
c = C()
a.callFunction(c) # How 'c' know that is called by an instance of A...
b.callFunction(c) # ... or B

Despite the design or other issues, this is only a question of an inquiring mind.

Note: This has to be done without changing otherFunction signature

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光匆匆的小流年 2024-08-31 18:20:00

如果这是出于调试目的,您可以使用inspect.currentframe():

import inspect

class C:
    def otherFunction(self):
        print inspect.currentframe().f_back.f_locals

以下是输出:

>>> A().callFunction(C())
{'self': <__main__.A instance at 0x96b4fec>, 'obj': <__main__.C instance at 0x951ef2c>}

If this is for debugging purposes you can use inspect.currentframe():

import inspect

class C:
    def otherFunction(self):
        print inspect.currentframe().f_back.f_locals

Here is the output:

>>> A().callFunction(C())
{'self': <__main__.A instance at 0x96b4fec>, 'obj': <__main__.C instance at 0x951ef2c>}
茶底世界 2024-08-31 18:20:00

这是一个快速技巧,获取堆栈并从最后一帧获取局部变量以访问自身

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

import inspect

class C:
    def otherFunction(self):
        lastFrame = inspect.stack()[1][0]
        print lastFrame.f_locals['self'], "called me :)"

c = C()

A().callFunction(c)
B().callFunction(c)

输出:

<__main__.A instance at 0x00C1CAA8> called me :)
<__main__.B instance at 0x00C1CAA8> called me :)

Here is a quick hack, get the stack and from last frame get locals to access self

class A:
    def callFunction(self, obj):
        obj.otherFunction()

class B:
    def callFunction(self, obj):
        obj.otherFunction()

import inspect

class C:
    def otherFunction(self):
        lastFrame = inspect.stack()[1][0]
        print lastFrame.f_locals['self'], "called me :)"

c = C()

A().callFunction(c)
B().callFunction(c)

output:

<__main__.A instance at 0x00C1CAA8> called me :)
<__main__.B instance at 0x00C1CAA8> called me :)
丢了幸福的猪 2024-08-31 18:20:00

使用 inspect 模块inspect 检查堆栈.stack()。然后,您可以使用 f_locals['self'] 从列表中的每个元素获取实例

Examine the stack with the inspect module with inspect.stack(). You can then get the instance from each element in the list with f_locals['self']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文