Undefined behaviour in C [was Re: The Cost of Dynamism]

Ned Batchelder ned at nedbatchelder.com
Sun Mar 27 11:48:10 EDT 2016


On Sunday, March 27, 2016 at 10:43:49 AM UTC-4, BartC wrote:
> On 27/03/2016 14:47, Dennis Lee Bieber wrote:
> > On Sun, 27 Mar 2016 12:31:26 +0100, BartC <bc at freeuk.com> declaimed the
> > following:
> >
> >> On 27/03/2016 07:34, Paul Rubin wrote:
> >>> BartC <bc at freeuk.com> writes:
> >>>>    But my suggestion was to have required a keyword in front of
> >>>> such expressions.
> >>>
> >>> Should there be a keyword in front of a line containing "sqrt(x)" ?
> >>> What about "launch(missiles)" ?
> >>
> >> They both look like function calls. Function calls are *very* commonly
> >> used as standalone expressions.
> >>
> >> You /could/ stipulate that they be written:
> >>
> >>    call launch(missiles)    # like Fortran iirc
> >>
> > 	Except that FORTRAN also explicitly defines them as
> >
> > 		subroutine xxx()
> > vs
> > 		return-type function yyy()
> >
> > and will generate a compile error if you "call" the latter, or put the
> > former in-line of an expression
> >
> > 	This would be the equivalent of removing "def" from Python, and adding
> > two new keywords: "sub" and "fun"; modifying the behavior so that "fun"s
> > must have an explicit return statement (preferably with an explicit return
> > value), whereas "sub"s have no return and just ... end...
> 
> Well, that could be done in Python (not so usefully because you can't 
> take account of such info until a call is attempted), but that's not 
> what I'm talking about, which is simply allowing:
> 
>    fn(...)
> 
> whether fn has an explicit return or not, and not allowing:
> 
>    fn         # and other kinds of expression
> 
> unless some keyword is used. (I've no idea what that might be; all the 
> best ones are taken. But I've already said a keyword can be emulated via 
> a dummy function call.)

Python *could* have made it an error to have a useless expression as a
statement.  It would prevent certain kinds of errors.  But it would also
complicate the language.  How do we decide what is a "useless expression"?

As we've seen this might be an expression with a side-effect:

    foo.bar

though it would be unusual.  Should it be forbidden?

And how do we make docstrings, which are simply string literals as
statements?  Or do we allow those?  Perhaps we only allow those if they
are the first statement in a module, class, or function?  How complicated
do we want these criteria to become?

One of Guido's principles in designing Python was to keep it simple,
even where that might mean people could make errors with it.  This part
of the language is no different: any expression can be a statement.

Any real language is designed with a number of competing factors in mind.
It isn't a simple matter.  Usually the answer to, "why was it done this
way?" is, "Because the designer had a different set of criteria, ordered
differently, than you do."  Python cares about preventing errors.  It
also cares about simplicity of design.

--Ned.



More information about the Python-list mailing list