[Python-ideas] Quick idea: defining variables from functions that take the variable name

Bruce Leban bruce at leban.us
Mon May 30 21:59:46 EDT 2016

On May 30, 2016 6:20 PM, "Guido van Rossum" <guido at python.org> wrote:
> In mypy we have a need for type variables, which are created like this:
>   from typing import TypeVar
>   T = TypeVar('T')
> I just saw a lightning talk about sympy where they define symbols to
> be used in mathematical equations, like this:
>   from sympy import Symbol
>   x = Symbol('x')
> I'm sure this is not a new idea, but so far I've always thought that
> this is pretty esoteric and the approach here is good enough. But
> maybe we can actually do better....

One of the negatives of the above usages is that the two names don't have
to match. I can do:

Q = TypeVar('U')

for example. Dedicated syntax could fix that, e.g.,

from typing import TypeVar as T$

And instead of writing x I write T$x. That compiles to a call to
TypeVar('x'). A function imported this way would be required to always
return the same value. That is T$xyz is T$xyz and the compiler would be
free to optimize calls away or not. If I want a runtime eval that won't be
optimized away I can write T$('x' + 'yz') which is T$xyz.

Of course $ could be :: or ! or something else.x

Nothing prevents me from writing

x = T$x

but I can be assured that this is the same as T$x elsewhere (barring
different code importing different functions under the same name).

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160530/bcc9c1bd/attachment-0001.html>

More information about the Python-ideas mailing list