[Python-ideas] Backtick expression: similar to a shorter lambda syntax

Steven D'Aprano steve at pearwood.info
Mon Jan 21 01:56:17 EST 2019


On Sun, Jan 20, 2019 at 07:21:50PM -0500, James Lu wrote:

> Backtick expressions work exactly like lambdas, except that they are 
> bound to the instance they are created in every time that class is 
> used to create one. To illustrate, this “percent” property is bound to 
> the instance, not to the class.
> class Example:
> percent = property(`self.v*self.v2/100`)

Sorry, that example is not clear to me. What does it do? There is no 
instance "self" to bind to at this point.

> And a few more examples for clarity.
> 
> def example():
> locals()['a'] = 1
> expr = `a+1`
> return expr() # error: one variable is required


Still not clear to me. It might help if you showed expected input and 
output, rather than expecting us to guess.

> Any variable names that exist when the backtick expression is created 
> are bound to the expression, 

I don't know what you mean by binding a name to an expression.

> and the reference to the expression is 
> stored within the expression. 

So it forms a reference loop? The expression stores a reference to 
itself? Why?


> Names that do not exist when the 
> expresssion is created must be passed in as parameters.

That's different behaviour from regular functions, where names are only 
resolved when the function is called.

> Such names can 
> also be passed in as keyword arguments. Backtick expressions are 
> created when their scope is created.

Created when their scope is created? So not when the line containing the 
expression is executed?

> Variable names that are declared but have not been assigned to will be 
> considered to exist for the purposes of the backtick expression.

Python doesn't have variable declarations, so I don't know what this 
means.

> Directly calling a backtick expression as soon as it’s created is forbidden:

Why?

> `v+1`(a)
> 
> But this is technically allowed but discouraged, like how := works:
> (`v+1`)(a)

How is that different? You're still directly calling the expression.

[...]
> I propose syntax highlighters show a backtick expression on a 
> different background color, a lighter shade of black for a dark theme; 
> dirty white for a light thing.

Nothing to do with us, or you for that matter. Editors are free to use 
whatever syntax highlighting they like, including none at all, and to 
allow users to customise that highlighting.

It disturbs me that you believe you get to tell everyone what syntax 
highlighting they should use for this feature. That's pretty 
dictatorial, and not in a good BDFL way.


-- 
Steve


More information about the Python-ideas mailing list