A Suggestion for Python Colon Syntax

Thomas Wouters thomas at xs4all.net
Sat Dec 23 15:36:49 EST 2000


> In article <CF316.26760$1t.879107 at typhoon.ne.mediaone.net>,
> David Abrahams <abrahams at mediaone.net> wrote:
> >"Tim Peters" <tim.one at home.com> wrote in message
> >news:mailman.977544967.21073.python-list at python.org...
> >>
> >> 2. The NumPy people had a real problem in that
> >>     x = x + y
> >> could end up generating multimegabyte temp arrays while
> >>     x += y
> >> need not.  That is, for them it wasn't "just another way" to write the
> >> former; it offers crucially different semantics.
> >
> >Yes! Any chance of getting this capability for extension classes?

Extention classes have this capability, ever since Python 2.0. If you look
at 'object.h' in your Python source tree, you'll notice that since 1.6 the
PyNumberMethods struct grew 11 more slots:

        binaryfunc nb_inplace_add;
        binaryfunc nb_inplace_subtract;
        binaryfunc nb_inplace_multiply;
        binaryfunc nb_inplace_divide;
        binaryfunc nb_inplace_remainder;
        ternaryfunc nb_inplace_power;
        binaryfunc nb_inplace_lshift;
        binaryfunc nb_inplace_rshift;
        binaryfunc nb_inplace_and;
        binaryfunc nb_inplace_xor;
        binaryfunc nb_inplace_or;

And PySequenceMethods grew two similar ones:

        binaryfunc sq_inplace_concat;
        intargfunc sq_inplace_repeat;

The use of these is guarded by the Py_TPFLAGS_HAVE_INPLACEOPS feature-flag,
so it won't break (backwards) binary compatibility unless you did something
stupid with the typeflags word (PyTypeObject.tp_flags). You should always
initialize the 'tp_flags' with Py_TPFLAGS_DEFAULT -- they don't signify that
you implemented support for a feature, but that your struct contains those
members, and thus that they can be tested for contents without causing
SEGVs.

Each of these inplace 'methods' should be functions that act exactly like
the non-inplace versions of the same methods: they should take two
arguments, 'self' and 'other', and it should return a new reference to the
result object. Note that I said 'new reference' -- if you return 'self', you
have to INCREF it before returning. 

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list