[Python-ideas] Fwd: Define a method or function attributeoutsideof a class with the dot operator
Mark E. Haase
mehaase at gmail.com
Sun Feb 12 12:56:38 EST 2017
On Sun, Feb 12, 2017 at 11:51 AM, Markus Meskanen <markusmeskanen at gmail.com>
wrote:
>
> 2. To register callbacks to objects, i.e. plain out set an attribute for
> an instance. I've used the menu example above:
>
> class Menu:
> def __init__(self, items=None, select_callback=None):
> self.items = items if items is not None else []
> self.select_callback = select_callback
>
> my_menu = Menu(['Pizza', 'Cake', 'Pasta'])
> def my_menu.select_callback(item_index):
> if item_index == 0: # Pizza
> serve_food(pizza)
> else: # Cake or Pasta
> ...
> This is just one example of using it to set an instance's variable to a
> callback. It's just shorthand for:
>
> def select_callback(item_index):
> ...
> my_menu.select_callback = select_callback
>
One issue that has been overlooked so far in this thread is that
hypothetical use cases are not as important as real-world use cases. One
way that PEPs can demonstrate real-world relevance is by demonstrating the
effect on some important libraries, e.g. the standard library.
For example, asyncio.Future (and concurrent.futures.Future) has a list of
callbacks and the API has add_done_callback() and remove_done_callback()
functions for manipulating the callback list. The proposed syntax doesn't
cooperate with these callbacks:
f = asyncio.Future()
def my_callback(x):
...
f.add_done_callback(my_callback)
How should I write this using the proposed syntax? If the proposal doesn't
generalize well enough to cover existing callback patterns in Python's own
standard library, then that is a significant weakness.
Please keep this in mind as you write the PEP.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170212/c3950bb2/attachment.html>
More information about the Python-ideas
mailing list