[Python-porting] Pickling unbound methods on Python 3

cool-RR cool-rr at cool-rr.com
Sat May 29 19:59:13 CEST 2010


On Sat, May 29, 2010 at 6:56 PM, M.-A. Lemburg <mal at egenix.com> wrote:

> cool-RR wrote:
> > On Sat, May 29, 2010 at 5:46 PM, R. David Murray <rdmurray at bitdance.com
> >wrote:
> >
> >> On Fri, 28 May 2010 21:55:28 +0200, cool-RR wrote:
> >>> One person told me that given an unbound method in Python 3.x, it's
> >>> *impossible* to tell to which class it belongs. Is it true?
> >>
> >> I believe that that is true.  In Python3 there is no such thing as an
> >> unbound method as a distinct object type.  There are functions, and
> >> there are bound methods.  See the second sentence in this section:
> >>
> >>
> >>
> http://docs.python.org/release/3.0.1/whatsnew/3.0.html#operators-and-special-methods
> >
> >
> > I see. Where would be a good place to discuss this decision? I would want
> > 3.2 to allow pickling of unbound methods.
>
>
Unbound methods don't exist in Python3. You only have functions and
> (bound) methods.
>

I know that unbound methods are of the function type in Python 3. I'm
calling them unbound methods because they are methods of classes which are
not bound to an instance. I'm aware they have no special stance, but I still
refer to them as unbound methods.

You can see that if you try to call an unbound method with a
> non-instance first arg:
>
> >>> class X:
> ...  def test(self): return 42
> ...
> >>> X.test(X())
> 42
> >>> X.test(3)
> 42
>
> Doing the same in Python2 gives an error:
>
> >>> X.test(X())
> 42
> >>> X.test(3)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unbound method test() must be called with X instance as first
> argument (got int instance
> instead)
>

Thanks for the interesting example, Marc-Andre.

Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20100529/40c8fc30/attachment-0001.html>


More information about the Python-porting mailing list