重写Python中的静态方法

发布于 2024-07-20 04:07:25 字数 851 浏览 6 评论 0原文

参考第一个答案 python的绑定和非绑定方法在这里,我有一个问题:

class Test:
    def method_one(self):
        print "Called method_one"
    @staticmethod
    def method_two():
        print "Called method_two"
    @staticmethod
    def method_three():
        Test.method_two()
class T2(Test):
    @staticmethod
    def method_two():
        print "T2"
a_test = Test()
a_test.method_one()
a_test.method_two()
a_test.method_three()
b_test = T2()
b_test.method_three()

产生输出:

Called method_one
Called method_two
Called method_two
Called method_two

Is there a way to override a static method in python?

我期望 b_test.method_ Three() 打印“T2”,但它没有(而是打印“Called method_two”)。

Referring to the first answer about python's bound and unbound methods here, I have a question:

class Test:
    def method_one(self):
        print "Called method_one"
    @staticmethod
    def method_two():
        print "Called method_two"
    @staticmethod
    def method_three():
        Test.method_two()
class T2(Test):
    @staticmethod
    def method_two():
        print "T2"
a_test = Test()
a_test.method_one()
a_test.method_two()
a_test.method_three()
b_test = T2()
b_test.method_three()

produces output:

Called method_one
Called method_two
Called method_two
Called method_two

Is there a way to override a static method in python?

I expected b_test.method_three() to print "T2", but it doesn't (prints "Called method_two" instead).

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

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

发布评论

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

评论(3

世态炎凉 2024-07-27 04:07:25

在您在那里使用的形式中,您明确指定要调用哪个类的静态method_two。 如果 method_third 是一个类方法,并且您调用了 cls.method_two,您将得到您想要的结果:

class Test:
    def method_one(self):
        print "Called method_one"
    @staticmethod
    def method_two():
        print "Called method_two"
    @classmethod
    def method_three(cls):
        cls.method_two()

class T2(Test):
    @staticmethod
    def method_two():
        print "T2"

a_test = Test()
a_test.method_one()  # -> Called method_one
a_test.method_two()  # -> Called method_two
a_test.method_three()  # -> Called method_two

b_test = T2()
b_test.method_three()  # -> T2
Test.method_two()  # -> Called method_two
T2.method_three()  # -> T2

In the form that you are using there, you are explicitly specifying what class's static method_two to call. If method_three was a classmethod, and you called cls.method_two, you would get the results that you wanted:

class Test:
    def method_one(self):
        print "Called method_one"
    @staticmethod
    def method_two():
        print "Called method_two"
    @classmethod
    def method_three(cls):
        cls.method_two()

class T2(Test):
    @staticmethod
    def method_two():
        print "T2"

a_test = Test()
a_test.method_one()  # -> Called method_one
a_test.method_two()  # -> Called method_two
a_test.method_three()  # -> Called method_two

b_test = T2()
b_test.method_three()  # -> T2
Test.method_two()  # -> Called method_two
T2.method_three()  # -> T2
↙温凉少女 2024-07-27 04:07:25

您看到的行为是预期的行为。 静态方法是...静态的。 当你调用Test中定义的method_third()时,它肯定会调用Test中定义的method_two()

至于如何“绕过”这种正确的行为......

最好的方法是当您想要虚拟行为时将方法设为虚拟。 如果您被某些具有静态方法的库代码所困扰,而您希望该静态方法是虚拟的,那么您可能会更深入地了解是否有原因,或者是否只是一个疏忽。

否则,您可以在 T2 中定义一个新的 method_two() 来调用 T2.method_two()

The behavior you see is the expected behavior. Static methods are... static. When you call method_three() defined in Test it will certainly call method_two() defined by Test.

As for how to "get around" this proper behavior...

The very best way is to make methods virtual when you want virtual behavior. If you're stuck with some library code with a static method that you wish were virtual then you might look deeper to see if there's a reason or if it's just an oversight.

Otherwise, you can define a new method_three() in T2 that calls T2.method_two().

小耗子 2024-07-27 04:07:25

此外,如果您想在没有实例的情况下调用“虚拟静态”函数,您可以像这样进行:

  1. 将基类中的函数声明为非静态,如下所示:

    类基础: 
          def my_fun(自我): 
              print('my_fun 基地') 
    
      派生类(基类): 
          def my_fun(自我): 
              print('my_fun 派生') 
      
  2. 通过传递类类型(不是实例)来调用它,如下所示:

    Derived.my_fun(派生) 
      

注意,如果您有一个变量“class_type”,该变量仅在运行时已知,则这很有用。

Additionally, if you want to call the "virtual static" function without an instance, you could proceed like so:

  1. Declare the function in the base class non-static like so:

    class Base:
        def my_fun(self):
            print('my_fun base')
    
    class Derived(Base):
        def my_fun(self):
            print('my_fun derived')
    
  2. Call it by passing the class type, which is not an instance, like so:

    Derived.my_fun(Derived)
    

Note, this is useful if you have a variable "class_type", which is only known during run time.

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