[Python-ideas] PEP 484 change proposal: Allowing <at> overload outside stub files

Stefan Krah skrah.temporarily at gmail.com
Sat Jan 23 16:12:26 EST 2016


Nick Coghlan <ncoghlan at ...> writes:
> You're already going to have to allow this for single lines to handle
> Py2 compatible annotations, so it seems reasonable to also extend it
> to handle overloading while you're still figuring out a native syntax
> for that.

I find that https://pypi.python.org/pypi/multipledispatch looks quite
nice:

>>> from multipledispatch import dispatch
>>> @dispatch(int, int) 
... def add(x, y):
...      return x + y
... 
>>> @dispatch(float, float)
... def add(x, y):
...     return x + y
... 
>>> add(1, 2)
3
>>> add(1.0, 2.0)
3.0
>>> add(1.0, 2)
Traceback (most recent call last):
  File
[cut because gmane.org is inflexible]
line 155, in __call__
    func = self._cache[types]
KeyError: (<class 'float'>, <class 'int'>)



Stefan Krah






More information about the Python-ideas mailing list