python - Adding a Method to an Existing Object Instance -


I've read that it is possible to add an existing object to a method (not in class definition, for example) in Python .

I think it is not always good to do this. But how can that be?

Looks good, I tried to do it, but it appears that it does not end in a truth method.

  From some other products. Import some modules Some speak class DF (self): "Oak ok EEEEEE!" SomeClass.speak = speak  

This example defines the new patch function with the argument of self , but if you use the actual code By the way, now the compromise class system asks for a logic called self (it does not automatically identify as the object that it is considered to bind, if that Class is defined within the definition), which means that you must class.patch () instead of class.patch (obj) you want the same functionality as an correct method.

It seems that Python does not really treat it as a method, but as much as it does, there is a function as a variable (and like That is callable). Is there any way to engage the actual method in a class?

In Python, there is a difference between functions and bound methods.

  & gt; & Gt; & Gt; Def foo (): ... print "foo" ... & gt; & Gt; & Gt; Category A: ... def bar (self): ... print "bar" ... & gt; & Gt; & Gt; A = A ()> & Gt; & Gt; Foo & lt; Function Fu 0x00A98D70 & gt; & Gt; & Gt; & Gt; A.bar & lt; Force Law EBL & lt; __ Main __. An example on 0x00A 9BC88 & gt; & Gt; & Gt; & Gt; & Gt;  

Bund method will be passed as the first argument in the form of a method called "Bound" (how descriptive), and whenever that instance is called.

Cables that are properties of a class (opposing an example) are still unbound, however, you can modify the class definition whenever you want:

  & Gt; & Gt; & Gt; Def fooFighters (self): ... print "fooFighters" ... & gt; & Gt; & Gt; Effufferes = FooFllder & gt; & Gt; & Gt; A2 = A ()> gt; & Gt; & Gt; A2.fooFighters & lt; Force Law Fuufitters & lt; __ Main __. An example at 0x00A 9BEB8 & gt; & Gt; & Gt; & Gt; & Gt; A2.fooFighters () fooFighters  

Already defined examples are updated (unless they override the attribute themselves):

 < Code> & gt; & Gt; & Gt; A.fooFighters () fooFighters  

The problem occurs when you want to add an example to a method:

  gt; & Gt; & Gt; Def Barfitter (self): ... print "barfinder" ... & gt; & Gt; & Gt; A.barFighters = Barfights & gt; & Gt; & Gt; A.barFighters () traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; TypeError: barFighters takes exactly 1 argument (0 given)  

The function is not automatically bound when it is directly linked to an example:

 < Code> & gt; & Gt; & Gt; A.barFighters & lt; Function at barFighters 0x00A98EF0 & gt;  

To pack it, we can use it:

  & gt; & Gt; & Gt; Import Type & gt; & Gt; & Gt; A.barFighters = types.MethodType (Barfighters, A)> and gt; & Gt; A.barfighters & lt; Bound method ?. The Barfighters & lt; __ Main __. An example on 0x00A 9BC88 & gt; & Gt; & Gt; & Gt; & Gt; A.barFighters () Barfighters  

This time other examples of class are not affected:

  & gt; & Gt; & Gt; A2.barFighters () traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; AttributeError: There is no attribute in an example 'barfielder'  

More information can be found by reading more about.


Comments