<div dir="ltr"><div>3 years ago, I've proposed syntax<br><br></div>def name = expr<br><div class="gmail_extra"><br></div><div class="gmail_extra">for binding names to arbitrary objects<br><br><a href="https://mail.python.org/pipermail/python-ideas/2013-May/020501.html">https://mail.python.org/pipermail/python-ideas/2013-May/020501.html</a><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">2016-05-31 6:57 GMT+02:00 Joshua Morton <span dir="ltr"><<a href="mailto:joshua.morton13@gmail.com" target="_blank">joshua.morton13@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I like the arrow idea, another option is a syntax more in line with the one being discussed for dict unpacking, something like adding a __define__ dunder that returns a tuple of (name, value). So that say, Typevar's __define__ returns the tuple `(self.__name__, self)`. Which is then converted into an assignment. The downside of this being that its assignment overloading(which can be abused), but at least it can only be done when specifically asked for. The other downside is that the syntax<div><br></div><div>define TypeVar('T')  # and I assume we can also declare a list of them</div><div>def ident(t: T) -> T:</div><div>    return t</div><div><br></div><div>looks both foreign and a bit magical. Also new keyword, but I have a feeling that the same keyword could probably be used here and with dict unpacking. (the *really* big downside is for a library implementer to do something devious like have __define__ always return ("constant", self), or generally something that wasn't clear to the caller, because we get names appearing out of no where with no clear history, but my argument would be that we should trust libraries not to do that and say that it is a very bad thing)</div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">The upside is that it avoids the problem of Steven's implementation, since the name is decided internally by the callable.</span></div><div><br></div><div>Like with his example, define `Typevar('7')` and similar invalid names would throw an error, in this case probably a TypeError.</div><div><br></div><div>--Josh<div><div class="h5"><br><div><br></div><div><div class="gmail_quote"><div dir="ltr">On Mon, May 30, 2016 at 11:08 PM Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, May 30, 2016 at 06:16:26PM -0700, Guido van Rossum wrote:<br>
<br>
[...]<br>
>   T = TypeVar('T')<br>
>   x = Symbol('x')<br>
><br>
> I'm sure this is not a new idea, but so far I've always thought that<br>
> this is pretty esoteric and the approach here is good enough. But<br>
> maybe we can actually do better, and whatever solution we come up with<br>
> might also be useful for extracting attributes from an object or<br>
> values from a mapping?<br>
<br>
This comes up a lot and it would be nice to clean it up.<br>
<br>
T = type('T', bases, ns)<br>
Record = namedtuple('Record', fields)<br>
<br>
It came up in the discussion on dict unpacking:<br>
<br>
a, b, c = **dict  # unpacks keys 'a', 'b', 'c'<br>
<br>
which was rejected as too magical. But maybe it will seem less magical<br>
if we have a special assignment operator that takes the left hand symbol(s)<br>
and copies them to the right?<br>
<br>
How do you feel about an arrow operator?<br>
<br>
<br>
T -> TypeVar()<br>
x -> Symbol()<br>
T -> type(bases, ns)<br>
Record -> namedtuple(fields)<br>
<br>
In general:<br>
<br>
name [, name, ...] -> callable(...)<br>
<br>
where "callable" is some expression, e.g. a name, a dot lookup, etc.<br>
<br>
# okay<br>
name -> sympy.Symbol()<br>
<br>
<br>
<br>
The easy case<br>
-------------<br>
<br>
All assignment targets are plain names.<br>
<br>
The arrow operator takes the names, turns them into strings, and passes<br>
them to the callable on the right as a tuple of strings, given as the first<br>
positional argument.<br>
<br>
Since the most common case will be a single target, I think it is worth<br>
while to treat it as a special case and pass just a string:<br>
<br>
T -> Type()  # like Type('T')<br>
<br>
a, b, c -> Spam(arg)  # like Spam(('a', 'b', 'c'), arg)<br>
<br>
The callable being called doesn't need to know or care whether it is<br>
being called with the -> or = assignment. It just receives the name(s)<br>
as the first argument.<br>
<br>
This will mean that callables that take the name as (say) the second<br>
argument cannot be used with this syntax.<br>
<br>
Using the arrow operator with arbitrary expressions on the<br>
right will be a SyntaxError:<br>
<br>
x -> y + 1<br>
my_name -> spam*f()<br>
my_name -> f() or g()<br>
<br>
I reject that third example as it is unclear whether both f and g, or<br>
only f, get the name as first argument.<br>
<br>
But this is allowed:<br>
<br>
my_name -> f(g())  # like f('my_name', g())<br>
<br>
If f isn't *actually* a callable, you get a runtime TypeError as usual.<br>
<br>
<br>
The hard case<br>
-------------<br>
<br>
Assignment target(s) is not a plain name.<br>
<br>
spam[2].eggs().attr -> Symbol()<br>
<br>
<br>
I'm going to take the cowards way out and just say "we don't allow<br>
that".<br>
<br>
<br>
--<br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div></div></div></div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">闇に隠れた黒い力<br>弱い心を操る</div>
</div></div>