Useless expressions [was Re: Undefined behaviour in C]

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 29 03:12:31 EDT 2016


On Monday 28 March 2016 23:18, BartC wrote:

> On 28/03/2016 02:24, Steven D'Aprano wrote:

>> Don't think about floats and ints and strings. Think of complex objects
>> with operator overloading. You're probably thinking of `x + y`. Instead,
>> think of things like:
>>
>> graph + node
>> database + table
> 
> There's theory, and there's practice.
[...]
> So no matter how many potentially useful counter-examples you can dig
> up, non-function-call expressions are still going to be highly unusual.
> And suspect.

Sure. If you want to call it a "code smell", no problem. It's not 
necessarily bad, but it's worth a second look.


> That's why it's not going to be much loss to disallow them /in that form/.

When you design your own language :-) feel free to design it that way. 
Python is designed differently. You don't have to agree with the decision, 
but that is the decision. The Python interpreter will accept any legal 
expression, and not try to guess which ones do and don't have side-effects, 
or which ones might be in error. Expressions include single names, so even 
they are allowed.

If you want stricter rules, use a linter.

 
> (There are also docstrings, but until yesterday I didn't even know they
> were also expressions. Wikipedia says this:
> 
> "Python docstrings appear as a string literal (not an expression) as the
> first statement following the definition of functions..."
> 
> so their status is a little unclear. 

I think what Wikipedia means is that you cannot use an expression that 
evaluates to a string as a docstring:


py> def f():
...     "A string %s" % 23
... 
py> f.__doc__ is None
True


It must be a string literal.



-- 
Steve




More information about the Python-list mailing list