On Oct 13, 2019, at 14:50, Steve Jorgensen <[email protected]> wrote:
>
> Perhaps, in addition to the using registration, implement a `__push__` method
> convention, and then the generic function for pushing an item to a collection
> would look for `__push__`, `push`, `append`, or `add` and try the first of
> those that it finds on an object of a type that is not registered.
This is _also_ trivial to do with singledispatch. Just change the base function
to look up the protocol:
@singledispatch
def just_add_it(collection, value):
try:
collection.push(value)
except AttributeError:
raise TypeError(whatever string you want)
There are a few different variations you can do with the lookup and error
handling, depending on whether you want to exactly simulate normal special
method lookup, or be a little more lenient.
Any types you register overloads for will use those overloads; any other types
will use the `push` method protocol if it exists, and it’ll raise whatever
exception you want otherwise.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/HPAXRG4DH4MJ72KFF4PF6LTEOYQK2HC7/
Code of Conduct: http://python.org/psf/codeofconduct/