This article mainly introduces the Python class dynamic modification of the example method of the relevant data, the need for friends can refer to the following
Example methods for dynamic modification of Python classes
It is believed that many friends will want to modify the code of the programmed behavior when they are programming, and the most common way is to rewrite some methods of the parent class that do not satisfy the requirements through subclasses. Let's say the following example.
Class Dog: def bark (self): print ' woof! ' class Husky (Dog): def bark (self) print ' howl! '
We can modify our own code in this way, but how do we modify the third-party code? Of course, we can also write a subclass of our own, invoke the subclass's instance object to implement the modification, but this may introduce a series of other problems. So we have to figure out a way to replace the original object method in our own way, which is the "patching" approach we'll cover next.
To patch a class
If we want to add or modify the method of the object, the simplest way is to make a patch for the class. With the above example, if we want to write a new Howl method for our own Dog, we can define a new Howl function, add it to our class like the following code:
def newbark (self): print ' wrooof! ' def howl (self): print ' howl! ' # Replace an existing Methoddog.bark = Newbark # Add a new Methoddog.howl = Howl
Very simple, huh? But here are a few questions that need our attention. First, the methods in all instances of the modified class are updated, so the updated method not only exists in the newly created object, but all previously created objects will have the updated method, unless the original method is added instead of overwritten. Second, the method you modify or add should be bound to the object, so the first parameter of the method should be the object being called (here is the instance self of the class).
Patching class Instances
A single object can also be patched without affecting other instances of the class. But it's still a little tricky! First let's take a look at the following example.
def herd (self, sheep): self.run () Self.bark () self.run () Border_collie = Dog () Border_collie.herd = Herd
Then we'll try to invoke the new defined method:
Border_collie.herd (sheep) typeerror:herd () takes exactly 2 arguments (1 given) the problem with the previous code was that The herd isn't a bound method, just take a look at the following code:print border_collie.herd <function herd at 0xf9 C5f0>
Something went wrong! The cause of the error is that the object being called is not passed as the first argument to the function we wrote. Of course we can pass in the parameters ourselves, but it doesn't work in the scenario of the substitution class method. The correct solution to this problem is to use the Methodtype function in the module, we can look at the following example code:
Import Types Border_collie = Dog () Border_collie.herd = types. Methodtype (herd, Border_collie) print Border_collie.herd<bound method?. Herd of <__main__. Dog instance at 0x23c9518>> border_collie.herd (sheep)
Now that our method has been bound with the instance, it's done!
Summarize