[Python-ideas] Having unbound methods refer to the classes their defined on

cool-RR cool-rr at cool-rr.com
Tue Jun 1 19:36:10 CEST 2010


Hello,

I would like to raise an issue here that I've been discussing at
python-porting.

(And I'd like to preface by saying that I'm not intimately familiar with
Python's innards, so if I make any mistakes please correct me.)

In Python 2.x there was an "unbound method" type. An unbound method would
have an attribute `.im_class` that would refer to the class on which the
method was defined. This allowed users to use the `copy_reg` module to
pickle unbound methods by name. (In a similar way to how functions and
classes are pickled by default.)

In Python 3.x unbound methods are plain functions. There is no way of
knowing on which class they are defined, so therefore it's impossible to
pickle them. It is even impossible to tell `copyreg` to use a custom
reducer:
http://stackoverflow.com/questions/2932742/python-using-copyreg-to-define-reducers-for-types-that-already-have-reducers

(To the people who wonder why would anyone want to pickle unbound methods: I
know that it sounds like a weird thing to do. Keep in mind that sometimes
your objects need to get pickled. For example if you're using the
multiprocessing module, and you pass into it an object that somehow refers
to an unbound method, then that method has to be picklable.)

The idea is: Let's give unbound methods an attribute that will refer to the
class on which they were defined.

What do you think?


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100601/7a1e517b/attachment.html>


More information about the Python-ideas mailing list