syntax difference

Chris Angelico rosuav at gmail.com
Wed Jun 20 00:18:19 EDT 2018


On Wed, Jun 20, 2018 at 11:19 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Wed, 20 Jun 2018 07:52:31 +1000, Chris Angelico wrote:
>
>>> What do you do in python when a local function variable needs to retain
>>> its value? I'm sure it can do it, but I bet it's not as simple as how I
>>> do it.
>>>
>> What do you mean, "retain its value"? Do you mean the way closures work?
>
> I think that Bart means something like static variables in C. That's
> something I'd like to see Python get. The simplest work-around we have is
> to put the static variable in the function parameter list as if it were
> an argument of the function.

Ah. Yeah, that would be a plausible feature to add to Python. But in
C, a static variable is basically the same thing as a global variable,
except that its name is scoped to the function. There is only one of
it. What happens in Python? For instance:

def f():
    def g():
        static x = 0
        x += 1
        return x
    return g

Does the static variable exist once for each instance of g()? If so,
it'll behave like a closure variable; if not, it'll behave like a
global. Either way, I'm pretty much certain that people will expect
the other.

>>> Multi-level loop break? Separators in numbers? I think that one is
>>> finally in.
>>
>> Multi-level loop break is most commonly spelled "return".
>
> Not in languages that have a multi-level break.

Sure, but across all languages, it's most commonly spelled "return",
and I don't see very many people screaming for multi-level break.

>> In over two
>> decades of programming, the number of times I've needed to break out of
>> multiple loops without breaking out of an entire function can be counted
>> on the fingers of one hand. Specifically, three times. In nearly three
>> decades.
>
> Okay. The number of times I've wanted an asynchronous function so far has
> been zero, therefore the feature must be useless, right?
>
> *wink*

Fair criticism. However, his original statement was that these were
inherently useful features. Some of them are definitely useful (static
variables, maybe reference arguments); others are useful in some
languages but a terrible fit for something like Python (pointers); and
some are just not all that commonly needed. Compare the number of
times you use a for...else, the number of times you use multi-level
break, and the number of times you use slices with negative steps. Now
add all of them up, and compare to the number of times you use
subprocesses, or network calls (either the direct socket layer, or one
of the higher level features that's built on it, like HTTP download).
Both of those are highly practical features. Neither can be
implemented purely in the stdlib (unless you just punt on it by making
a totally unchecked way to make system calls). Both are, therefore,
extremely useful language features, even though they don't actually
require dedicated syntax.

ChrisA



More information about the Python-list mailing list