I thought I knew a bit of Python, but then I stumped on this (a distilled form follows):
class A:
@staticmethod
def f():
print('what code should be here?')
class B:
@staticmethod
def f():
A.f()
class C:
@staticmethod
def f():
A.f()
B.f() # I want this to print 'B'
C.f() # I want this to print 'C'
Note: compared to How can I get the address of the caller function?, this question describes a much more difficult situation (in my opinion). In fact, the referred question is easily solvable using an iteration over globals (see below).
Here’s what I tried:
-
If I use the
inspect
module I get access to the the caller function’s name as a str (not the caller function’s object itself), and so I can’t use the__qualname__
. -
If I try to iterate over the globals to see which global object has a method named ‘f’ I get two answers with no way to decide between the two.
-
I thought that an attribute ‘cls’ should be among the
locals
, just like ‘self’ would be if these were regular functions (not static methods), and if so I could get to the class byinspect.stack()[1][0].f_locals['cls']
, but ‘cls’ is not there.
Any ideas?