简体   繁体   English

如何在 python 3.x 中的同一 class 的命名空间内调用 class 方法

[英]How to call class method within namespace of the same class in python 3.x

When working with python instances, it is possible to access bound methods of the same class using self .使用 python 实例时,可以使用self访问相同 class 的绑定方法。 This resolves to a method corresponding to the same class in hierarchy.这解析为与层次结构中相同的 class 对应的方法。

class A:
    def f(self):
        return 1

    def __init__(self):
        self.v = self.f()

class B(A):
    def f(self):
        return 2

b = B()
# b.v is set to 2

But, when working with class methods, there is no apparent way of accessing methods of the same class as above.但是,当使用 class 方法时,没有明显的方法可以访问与上述相同的 class 方法。

In my use case, f above needs to be a class method and I need to set class variable v according to f corresponding to the same class.在我的用例中,上面的f需要是 class 方法,我需要根据对应于相同 class 的f设置 class 变量v Somewhat like:有点像:

class A:
    @classmethod
    def f(cls):
        return 1

    v = resolution_of_calling_class.f()

class B(A):
    @classmethod
    def f(cls):
        return 2

# B.v should be 2

edit : v is actually an attribute defined by parent class, which should find a method overridden by child class编辑v实际上是由父 class 定义的属性,它应该找到由子 class 覆盖的方法

You just need to override __new__ method, since it is invoked before the __init__ and its purpose is to create an instance, that will be initialized by __init__ .您只需要覆盖__new__方法,因为它在__init__之前调用,其目的是创建一个实例,该实例将由__init__初始化。

class A:

    def __new__(cls, *args, **kwargs):
        cls.v = cls.f()
        return super().__new__(cls, *args, **kwargs)

    @classmethod
    def f(cls):
        return 1


class B(A):
    @classmethod
    def f(cls):
        return 2


a = A()
print(a.v)

b = B()
print(b.v)
1
2

I am not 100% sure I understand what you are trying to do.我不是 100% 确定我理解你想要做什么。

I used your code above and我在上面使用了您的代码

class A:
    @classmethod
    def f(cls):
        return 1

class B:
    @classmethod
    def f(cls):
        return 2

print(B.f())

gives me 2 just as I expected it would.正如我预期的那样给我 2。 Should B be a child class of A as in the first example? B 应该是 A 的子 class 吗?

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

相关问题 Python 3.x:如何从类中保存和加载数据 - Python 3.x: How to save and load data from within a class 如何从python中相同的重载派生类方法中调用基类方法? - How do I call a base class method from within the same overloaded derived class method in python? 在Python 3.x中从另一个类调用类方法 - Calling Class Method From Another Class in Python 3.x Python:如何从同一类的类方法调用实例方法 - Python: How to call an instance method from a class method of the same class PYTHON:能够从同一类中调用方法有什么必要? - PYTHON: What is necessary to be able to call a method from within the same class? 如何从python中的类中的方法调用该类以重新定义变量? - how to call a the class from a method within the class in python to redefine a variable? Python:如何在类的方法内调用类的新实例 - Python: How to call a new instance of a class within a method of the class Python 3.X:如何调用类的函数来检查输入字符串的特征? - Python 3.X: How do you call a function of a class to check for the characterisitics of an input string? 从Python 2.x内调用Python 3.x程序的最佳方法是什么? - What is the best method to call a Python 3.x program from within Python 2.x? 类方法不能调用同一类中的其他类方法 - Class method can not call other class methods within the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM