python电话是否接通,Python – 我可以访问给我打电话的对象吗?

如果我有这个:

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签名的情况下完成此操作

解决方法:

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

import inspect

class C:

def otherFunction(self):

print inspect.currentframe().f_back.f_locals

这是输出:

>>> A().callFunction(C())

{'self': <__main__.a instance at>, 'obj': <__main__.c instance at>}

标签:python,oop,function-calls

来源: https://codeday.me/bug/20190713/1452280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值