[Python-3000] Binding builtin function to class

Terry Reedy tjreedy at udel.edu
Sun Apr 27 22:24:53 CEST 2008


"Haoyu Bai" <divinekid at gmail.com> wrote in message 
news:4814A8D0.9090900 at gmail.com...
| Hello,
|
| I'm a GSoC student working on SWIG's Python 3 support. When doing
| experiment on Python 3's new features, the different behavior between
| binding 'function' and 'builtin_function_or_method' confused me.
|
| As we know, unbound method is removed in Python 3. To bind a function to
| a class, we can directly use this instead:
|
| MyClass.myfunc = func

That was always possible.

| But in the case of builtin function, it can't work.

What is it that 'cannot work'?  My guess is that you are talking about the 
fact that instances do not get bound as an argument to the first parameter 
of a builtin.  This is also true in 2.5.2 (for instance):

>>> class T2(object): l=len
...
>>> t2=T2()
>>> t2.l()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: len() takes exactly one argument (0 given)

Your example with print did not throw an exception only because it allows 
no args.  (And, it cannot work in 2.x where print is a statement keyword.)

Or did you mean something else?

If *extension* function show a difference, perhaps SWIG needs revision for 
3.x.

[Builtin callables are also different in respect to parameter naming and 
binding args by keyword.  Perhaps to reduce confusion, they should not be 
named 'functions' in the manuals.]

tjr





More information about the Python-3000 mailing list