Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of Using MethodType in Python

What are the advantages of using MethodType from the types module? You can use it to add methods to an object. But we can do that easily without it:

def func():    print 1  class A:    pass  obj = A() obj.func = func 

It works even if we delete func in the main scope by running del func.

Why would one want to use MethodType? Is it just a convention or a good programming habit?

like image 239
Existent Avatar asked May 26 '16 08:05

Existent


People also ask

What is an instance function Python?

Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.

How do you call a class method in Python without self?

Class methods don't need self as an argument, but they do need a parameter called cls. This stands for class, and like self, gets automatically passed in by Python. Class methods are created using the @classmethod decorator.

What is the use of class method in Python?

A class method is a method which is bound to the class and not the object of the class. They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance. It can modify a class state that would apply across all the instances of the class.


Video Answer


2 Answers

In fact the difference between adding methods dynamically at run time and your example is huge:

  • in your case, you just attach a function to an object, you can call it of course but it is unbound, it has no relation with the object itself (ie. you cannot use self inside the function)
  • when added with MethodType, you create a bound method and it behaves like a normal Python method for the object, you have to take the object it belongs to as first argument (it is normally called self) and you can access it inside the function

This example shows the difference:

def func(obj):   print 'I am called from', obj class A:   pass a=A() a.func=func a.func() 

This fails with a TypeError: func() takes exactly 1 argument (0 given), whereas this code works as expected:

import types a.func = types.MethodType(func, a) # or types.MethodType(func, a, A) for PY2 a.func() 

shows I am called from <__main__.A instance at xxx>.

like image 114
mguijarr Avatar answered Oct 14 '22 21:10

mguijarr


A common use of types.MethodType is checking whether some object is a method. For example:

>>> import types >>> class A(object): ...     def method(self): ...         pass ... >>> isinstance(A().method, types.MethodType) True >>> def nonmethod(): ...     pass ... >>> isinstance(nonmethod, types.MethodType) False 

Note that in your example isinstance(obj.func, types.MethodType) returns False. Imagine you have defined a method meth in class A. isinstance(obj.meth, types.MethodType) would return True.

like image 33
Manuel Jacob Avatar answered Oct 14 '22 22:10

Manuel Jacob