Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overcoming Python's limitations regarding instance methods

It seems that Python has some limitations regarding instance methods.

  1. Instance methods can't be copied.
  2. Instance methods can't be pickled.

This is problematic for me, because I work on a very object-oriented project in which I reference instance methods, and there's use of both deepcopying and pickling. The pickling thing is done mostly by the multiprocessing mechanism.

What would be a good way to solve this? I did some ugly workaround to the copying issue, but I'm looking for a nicer solution to both problems.

Does anyone have any suggestions?

Update:

My use case: I have a tiny event system. Each event has an .action attribute that points to a function it's supposed to trigger, and sometimes that function is an instance method of some object.

like image 262
Ram Rachum Avatar asked Nov 25 '09 17:11

Ram Rachum


People also ask

What is Python instance method?

Instance methods are the default methods defined in Python classes. They are called instance methods because they can access instances of a class (objects). Two ways to use instance methods are to read or write instance attributes. In other words instance methods are used to read or modify the state of an object.

What is instance in Python quora?

In python a class is created by the keyword class . An object is created using the constructor of the class. This object will then be called the instance of the class. For example. class Snake: # class.


2 Answers

You might be able to do this using copy_reg.pickle. In Python 2.6:

import copy_reg
import types

def reduce_method(m):
    return (getattr, (m.__self__, m.__func__.__name__))

copy_reg.pickle(types.MethodType, reduce_method)

This does not store the code of the method, just its name; but that will work correctly in the common case.

This makes both pickling and copying work!

like image 169
Jason Orendorff Avatar answered Oct 20 '22 00:10

Jason Orendorff


REST - Representation State Transfer. Just send state, not methods.

To transfer an object X from A to B, we do this.

  1. A encode the state of X in some handy, easy-to-parse notation. JSON is popular.

  2. A sends the JSON text to B.

  3. B decodes the state of X form JSON notation, reconstructing X.

B must have the class definitions for X's class for this to work. B must have all functions and other class definitions on which X's class depends. In short, both A and B have all the definitions. Only a representation of the object's state gets moved around.

See any article on REST.

http://en.wikipedia.org/wiki/Representational_State_Transfer

http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

like image 45
S.Lott Avatar answered Oct 20 '22 01:10

S.Lott