[Python-Dev] Overloading comparison operator for lists
Cameron Simpson
cs at cskk.id.au
Mon May 27 00:12:12 EDT 2019
This belongs on python-ideas, not python-dev. I've directed replies to
this message there. Comments below.
On 26May2019 21:52, Montana Burr <montana.burr at gmail.com> wrote:
>NumPy arrays have this awesome feature, where array == 3 does an
>element-wise comparison and returns a list. For example:
>
>np.array([1,2,3,4,5])==3
>
>returns
>
>[False,False,True,False,False]
>It would be cool if Python had similar functionality for lists.
map(lamdba item: item==3, [1,2,3,4,5])
I'm not sure this rates extra Python features.
Personally I'm -1 on this suggestion because == traditionally returns a
Boolean, NumPy notwithstanding. Your example above doesn't return a
Boolean.
>If that is not possible, perhaps we could consider allowing developers
>to overload operators on built-in types within the context of a project or
>module. For example, an overload in one module would have no effect on the
>same operator in a different module (such as any Python standard modules.)
This is usally done by overloading dunder methods on classes. if you
class subclasses a builtin eg int or list then the instances get the
special behaviour.
>Additionally, let's then give the developers the option to explicitly
>import an overload from other modules. So, people could develop a module
>with the purpose of providing overloads that make complete sense within a
>certain context.
If you go the subclass route you could do this with a mixin class (a
class providing methods but little else, intended to be part of the MRO
of a subclass).
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-Dev
mailing list