<div dir="ltr"><div dir="auto"><div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="m_-750829056919088455m_4689240330794853406m_4442173380105353836quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I will say, though, that you're right that we've over-reacted a bit to<br>
the monkeypatching use case. Although maybe that's because no-one can<br>
think of many *other* use cases that they'd need the new syntax for<br>
:-)<br>
<font color="#888888"><br>
Paul</font></blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">Hi Paul, I believe at least two other use cases than monkey patching have been mentioned already:</div><div dir="auto"><br></div><div dir="auto">1. Allowing the class to be used in the method's header, f.e. for typing and decorators:</div><div dir="auto"><br></div><div dir="auto"> @decorate(MyClass)</div><div dir="auto"> def MyClass.method(self, other: MyClass) -> List[MyClass]:</div><div dir="auto"> ...</div><div dir="auto"><br></div><div dir="auto">This is useful since you can't refer the class itself inside of its body. At the moment the way to use typing is to write the class's name as a string... It feels awful.</div><div dir="auto"><br></div><div>2. To register callbacks to objects, i.e. plain out set an attribute for an instance. I've used the menu example above:<br><br></div><div> class Menu:</div><div> def __init__(self, items=None, select_callback=None):</div><div> self.items = items if items is not None else []</div><div> self.select_callback = select_callback</div><div><br></div><div> my_menu = Menu(['Pizza', 'Cake', 'Pasta'])</div><div> def my_menu.select_callback(item_i<wbr>ndex):</div><div> if item_index == 0: # Pizza</div><div> serve_food(pizza)</div><div> else: # Cake or Pasta</div><div> ...</div><div><br></div><div>This is just one example of using it to set an instance's variable to a callback. It's just shorthand for:</div><div><br></div><div> def select_callback(item_index):</div><div> ...</div><div> my_menu.select_callback = select_callback</div><div><br></div><div>This reads much easier and saves us from typing the same thing three times (see decorators).</div><div dir="auto"></div></div>
</div>