[Python-ideas] Non-boolean return from __contains__

Terry Reedy tjreedy at udel.edu
Thu Jul 29 19:18:42 CEST 2010


On 7/27/2010 2:04 PM, Guido van Rossum wrote:
> On Tue, Jul 27, 2010 at 5:25 PM, Robert Kern<robert.kern at gmail.com>  wrote:
>> I've occasionally wished that we could repurpose backticks for expression
>> literals:
>>
>>   expr = `x + y*z`
>>   assert isinstance(expr, ast.Expression)
>
> Maybe you could just as well make it a plain string literal and call a
> function that parses it into a parse tree:
>
> expr = parse("x + y*z")
> assert isinstance(expr, ast.Expression)
>
> The advantage of this approach is that you can define a different
> language too...

and that is already exists, and is more visible than backticks

 >>> def expr(s): return ast.parse(s, mode='eval') # defaults is 'exec'

 >>> e = expr('a+b')
 >>> e
<_ast.Expression object at 0x00F8DCF0>

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list