简体   繁体   English

获取继承函数的 __qualname__

[英]Get __qualname__ of inherited function

class A:
    def func(self):
        print('This was in A')

class B(A):
    def text(self):
        print('This is in B')

print(B.func.__qualname__)

The output of this would be A.func but I'd like it to be B.func这个的输出将是A.func但我希望它是B.func
It would be like functools.wraps but for class functions.它就像 functools.wraps 但对于类函数。

Seems that not so good solution, but it can help you:似乎不是很好的解决方案,但它可以帮助您:

class A:
    def func(self):
        print('This was in A')


class B(A):
    def text(self):
        print('This is in B')

    def func(self):
        return super().func()


print(B.func.__qualname__)

I'm not sure how exactly you'll be using this in logging, it would help if you could elaborate on your question.我不确定你将如何在日志中使用它,如果你能详细说明你的问题会有所帮助。 Here I assume what you want is a function (named print_func ), that when passed with an instance method, prints the method name and class name of the actual instance.在这里,我假设您想要的是一个函数(名为print_func ),当与实例方法一起传递时,会打印实际实例的方法名称和类名称。

In this case, you can just separately print the instance class name and the method name, for example:在这种情况下,您可以单独打印实例类名称和方法名称,例如:

class A:
    def func(self):
        print('This was in A')

class B(A):
    def text(self):
        print('This is in B')

def print_func(f):
    print(f.__self__.__class__.__qualname__, ".", f.__name__)

b = B()
print_func(b.func)  # B . func

The __self__ attribute of a bound instance method gives the instance that the method is bound to (in this case, b ), and __class__ of an instance gives the class of the instance.绑定实例方法的__self__属性给出了方法绑定到的实例(在本例中为b ),实例的__class__给出了实例的类。

You can also extend the print_func method by checking whether f is a bound instance method, and add additional routines for normal functions, static methods, and class methods.您还可以通过检查f是否为绑定实例方法来扩展print_func方法,并为普通函数、静态方法和类方法添加额外的例程。 An example would be:一个例子是:

import inspect

def print_func(f):
    if inspect.ismethod(f):
        # Could be instance method or class method.
        self = f.__self__
        if inspect.isclass(self):
            print("classmethod", self.__qualname__ + "." + f.__name__)
        else:
            print(self.__class__.__qualname__ + "." + f.__name__)
    else:
        # The method is either an unbound instance method, a static method,
        # or a normal function. Either case, it is not bound to an instance,
        # so `__qualname__` should suffice.
        # It would be non-trivial to print the actual class from which we
        # accessed this function. See examples marked with asterisks (*) below.
        print(f.__qualname__)

To see it in action:要查看它的实际效果:

class A:
    def method(self): pass

    @classmethod
    def classmethod(cls): pass

    @staticmethod
    def staticmethod(): pass

class B(A):
    pass

def function(): pass

a = A()
b = B()

print_func(A.method)  # A.method
print_func(B.method)  # A.method  *
print_func(a.method)  # A.method
print_func(b.method)  # B.method

print_func(A.classmethod)  # classmethod A.classmethod
print_func(B.classmethod)  # classmethod B.classmethod
print_func(a.classmethod)  # classmethod A.classmethod
print_func(b.classmethod)  # classmethod B.classmethod

print_func(A.staticmethod)  # A.staticmethod
print_func(B.staticmethod)  # A.staticmethod  *

print_func(function)  # function

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

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