<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Fri, Sep 16, 2016 at 1:16 PM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Fri, Sep 16, 2016 at 12:10:22AM +0000, אלעזר wrote:<br>
<br>
[...]<br>
> Benefits of putting such a collection in stdlib (instead of as an external<br>
> package) include:<br>
<br>
Slow down! Before getting all excited about adding these typing hints(?)<br>
into typing.modifiers, you first have to convince people that they are<br>
useful and deserve a place in the std library.<br>
<br>
What problem are these hints/classes supposed to solve? What solutions already exist? Why aren't those solutions good enough?<br></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"></blockquote><div>It is partially described below: mainly, reasoning and readability. I will try to elaborate, and please forgive me if what I'm writing is obvious to everyone here. Note that by "readability" I mean "conventional, non-repetitive syntax, the allow human reasoning", not the subjective issue (which is also important).</div><div><br></div><div>Q. Why standard?</div><div><br></div><div><div>A. Python is not very good at allowing strict reasoning. The confidence in Python programs, insofar it exists, comes from its reliance on very strong conventions ("One Way To Do It") and mostly-explicit control flow and name binding. Data flow, however, is very much implicit since the language encourages mutation.</div><div> <br></div><div><div> Tools that employ conventions are generally linters; these are very useful in finding trivial bugs, but have zero guarantees about their absence - unless the inference is trivial. linters and IDEs can also use basic form of type information, but will not analyze it deeply; hence the need for convention for modifiers that themselves constrain mutability, subclassing, etc.As a gradually-typed language, constraints are opt-in at best, but there should be a way to opt-them-in in a conventional way. Type hints are very good, and the suggested modifiers complement them.</div></div></div><div><br></div><div>---</div><div>Q. <span style="line-height:1.5">What solutions </span><span style="line-height:1.5">already exist? Why aren't those solutions good enough?</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">A:</span></div><div>* Named: There are no conventions and no standard way to prohibit monkey-patching, since this flexibility is often useful. <span style="line-height:1.5">That's fine.</span><span class="inbox-inbox-Apple-converted-space" style="line-height:1.5"> </span><span style="line-height:1.5">But what if I want to tell the other programmer that this class should not be monkey patched, but its fields are mutable? Currently the general solution is something like</span></div><div><br></div><div> class Person:</div><div> name = None</div><div> id = None</div><div> def __init__(self, name, id):</div><div> <a href="http://self.name">self.name</a> = name</div><div> self.od = id</div><div><br></div><div>Say we ignore the repetition. Will tools catch the last typo? Some might warn, but how should a tool know that it is not intentional? There's no standard way to express this intent. mypy will say that's an error (if there are type annotations), but we get the other problem of *allowing* monkey patching.</div><div><span style="line-height:1.5">StackOverflow <a href="http://stackoverflow.com/questions/35988/c-like-structures-in-python">suggests</a> either NamedTuple (which is not what I want in this case, since it is immutable and indexable), another answer without __init__ (which requires useful default values, and no sign of the "non-mankey-patch" intention). another answer with arbitrary **kwargs assignment (very flexible, but zero reasoning). Yet another suggests `dict` which is useful but has nothing to do with reasoning.</span><br></div><div>There are many solutions in pypi, most only address the boilerplate issue; but the "attrs" package seems nice; it might fit in the alternative decorator-oriented framework I mentioned.</div><div>There isn't even a standard name for that feature. "Struct" means something else.<br></div><div><br></div><div>* Immutable: The importance to reasoning is obvious. There are many alternative suggestions; again, most upvoted is NamedTuple, which is great but also indexable. This means one kind of type error that will not be caught. (actually two, since the __init__ method takes positional arguments that can be mixed). Additionally, inheriting classes can be monkey-patched, and there's no standard way to disallow inheritance. The above problems can be solved in an ad-hoc manner, but it won't give tool support. Other solutions involve messing up with __setattr__ etc. <span style="line-height:1.5">It Again, "attrs" seems has a solution in the form of "freeze".</span></div><div><br></div><div>* Sealed: Useful for typechecker (prevent erroneus cast) and optimizers that need to know the actual implementation of method dispatch (I believe it can be used in CPython too, but that's a different topic).</div><div>The only solution I've found is this answer</div><div><a href="http://stackoverflow.com/questions/16564198/pythons-equivalent-of-nets-sealed-class">http://stackoverflow.com/questions/16564198/pythons-equivalent-of-nets-sealed-class</a><br></div><div>which gives a hand-crafeted metaclass. But if it's not conventional, it won't be used by public tools.</div><div><br></div><div>* Array: If I want a tuple with mutable cells, I currently simply don't have any option. There's list, which is extendable. There's array.array and numpy's matrices, which are intended for numeric processing. There might be some package for that, <span style="line-height:1.5">but I can't find it - </span><span style="line-height:1.5">and of course none that allow type annotations. Besides, there's no reason to assume it will work together with some other "Named" modifier implementation.</span></div><div><br></div><div>* Namespace (as a shorthand for Final+Abstract): "not instantiable" will help catch both erroneous instantiations (which is implementable by hand) and erroneous "isinstance" (which is not). It might be used as an equivalent to "object" in Scala.</div><div><br></div><div>----</div><div>My suggestion is (again) two steps mixed:</div><div>1. Add support for modifiers, by convention and library</div><div>2. Some concrete syntax, inspired by Enum and the new NamedTuple syntax.</div><div><br></div><div>Alternative syntax, using "attrs"-like decorators, might be:</div><div><br></div><div> @modifiers(<span style="color:rgb(33,33,33);font-family:monospace;font-size:13px;line-height:1.5">Immutable, Named)</span></div><div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5"><font face="monospace"> class Command:</font></span></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5"><font face="monospace"> cmd : str = "NOP"</font></span></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5"><font face="monospace"> value : int = None</font></span></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font face="monospace"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font face="monospace"> load = Command(cmd="load", value=1000)</font></div></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><br></div><div>But then `enum` and `NamedTuple` should be updated to match this convention.<span style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px">Again, even if this suggestion is not accepted right now, there should be some decision to avoid accumulating parts of it in a way that is not consistent and will not allow combinations. For example, If it the addition of modifiers was forced on the language by God, would it be in the form of decorators? if your answer is yes, then I believe the new NamedTuple form should become a decorator too, before it is too late (as in Enum).</span></div><div><span style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><br></span></div><div>--</div><div>P.S. how do I change the name in my quotes? I believe אלעזר is not very easy to address...</div><div><br></div><div>~Elazar</div></div></div>