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

Alan Cristhian alan.cristh at gmail.com
Wed Jun 1 16:48:31 EDT 2016


> > > # Your proposal.
> > > def T as (lambda: Typevar)()
> > > def x as (lambda: sympy.Symbol)()
> >
> > That's hardly fair. The two examples you quote are the ones that
> > really *do* work with the "one-argument function" restriction:
>
> Ah yes, you're right, I misread Alan's proposal. Typevar would work,
> because it is an expression that evaluates to a callable of one
> function. I read Alan as saying that the expression would be called.

I still have trouble with english :)

My proposal is:

    def x as <expression-that-creates-a-callable>  # single variable
    def x, y, z as <expression-that-creates-a-callable> # multiple variables

Where <expression-that-creates-a-callable> should be evaluated and assigned
to a temporary variable. E.g:

    def T as typing.TypeVar

Where "typing.TypeVar" it's what I mean with
<expression-that-creates-a-callable>.

Then the statement

   def T as typing.TypeVar

Could be semantically equivalent to

    _temporary = typing.TypeVar
    T = _temporary("T")

This solves the complex use cases when you need to use more than one
argument. For example, in sympy you can indicate that you want an integer
symbolic variable:

    x = Symbol("x", integer=True)

Whit my syntax you can do:

    SymbolicInteger = lambda name: Symbol(name, integer=True)
    def x as SymbolicInteger

But for me, this is useless. When I use sympy, in all cases I assign more
than one variable because the "Functions of Several Variables" are very
common. If you go to the live interactive sympy console (
http://live.sympy.org) you will find this tree lines:

   >>> x, y, z, t = symbols('x y z t')
   >>> k, m, n = symbols('k m n', integer=True)
   >>> f, g, h = symbols('f g h', cls=Function)

That is the reason by which I propose the alternative whit multiple
variables. Sympy have the "Symbol" class that creates one symbolic variable
and the "symbols" function that creates multiple variables at once.

The full proposal is:

    def x, y, z as <expression-that-creates-a-callable>

That should be semantically equivalent to

    _temporary = <expression-that-creates-a-callable>
    x = _temporary("x")
    y = _temporary("y")
    z = _temporary("z")

I think that my proposal solves more use cases, don't need new keywords and
is more like english grammar.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160601/c9b78f92/attachment.html>


More information about the Python-ideas mailing list