Are Python methods overridden?

Asked 2 years ago, Updated 2 years ago, 34 views

Java states that the static method cannot be overridden.
For example, the following code is not an override:
(Assumed to be hiding, not overriding)

class A {
    public static voida() { 
        System.out.println("A.a()");
    }
}   

class Bextends A {
    public static voida() {
        System.out.println("B.a()");
    }
}

On the other hand, try writing a similar code in Python.

class A(object): 
    @staticmethod
    defa():
        print("A.a()"

Class B(A):
    @staticmethod
    defa():
        print("B.a()"

Is this an override?

question:

  • Should the Python code example above be considered an override? What is the basis for the decision?
  • What about classmethods or normal methods instead of staticmethods?
  • What should I think of dynamic language in general?

java python

2022-09-30 19:44

2 Answers

It's a little rough, but please use it as a link until you get a better answer.

Should the Python code example above be considered an override?What is the basis for the decision?

You can think about it.The following code runs B.a() when you add the instance method call_a() to call the static method a() to the base class A and call call_a() to the instance of B.This is the basis.

class A(object): 
    @staticmethod
    defa():
        print("A.a()"

    def call_a(self):
        self.a()

Class B(A):
    @staticmethod
    defa():
        print("B.a()"

p = B( )
p.call_a()#B.a()

What about classmethods or normal methods instead of staticmethods?

Experiment with classmethod in a similar way. Normal methods can of course be considered overrides.

What should I think of dynamic language in general?

Going back to the basics of OOP, "you can override" means... You can change = override the existing behavior for a message.

And the first example is to look at it. If call_a() also calls A.a() for an instance of B, This means that the behavior could not be changed. You have not been able to override it.


2022-09-30 19:44

Override means customizing behavior by replacing methods, and hyping means that the name of a narrow scope takes precedence over the name of a wide scope, not limited to classes, and that the normal way is not to look at the wide scope side.

The reason Java sample code is not overridden is that class B a method is not called and the customization fails in the following situations:

class A {
    public static voida() { 
        System.out.println("A.a()");
    }
    public static void b() {
        a();
    }
}   

class Bextends A {
    public static voida() {
        System.out.println("B.a()");
    }
}

B.b();//->A.a() appears.

On the other hand, this code is hyped because B.a() is called instead of A.a() when a() is called normally in class B.

In the case of Python staticmethod, in KoRoN's example, the customization was successful, but the customization failed when call_a() was set to staticmethod.Therefore, whether this is overridden or not depends on how you call a().

class A(object): 
    @staticmethod
    defa():
        print("A.a()"

    @staticmethod
    def call_a():
        A.a()

Class B(A):
    @staticmethod
    defa():
        print("B.a()"

p = B( )
p.call_a()#A.a()

On the other hand, Python classmethods can be called complete overrides because they have been successfully customized even if the caller is classmethod, and the purpose of using classmethods instead of staticmethods is to enable customization.

In this way, I think it's better to judge whether "override or high" is intended for customization or not.

By the way, the following cases are clearly not overrides, but are elevated.

x=3

defa(x):
    print x

a(5)#5


2022-09-30 19:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.