Benefits of unicode identifiers (was: Allow additional separator in identifiers)

Mikhail V mikhailwas at gmail.com
Thu Nov 23 15:38:01 EST 2017


On Thu, Nov 23, 2017 at 8:15 PM, Chris Angelico <rosuav at gmail.com> wrote:

>
> Let's start with a simpler question. Which of these is better code?
>
> # ====== Option 1
> class ZipExhausted(Exception):
>     pass
>
> def zip_longest(*args, **kwds):
>     # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
>     fillvalue = kwds.get('fillvalue')
>     counter = len(args) - 1
>     def sentinel():
>         nonlocal counter
>         if not counter:
>             raise ZipExhausted
>         counter -= 1
>         yield fillvalue
>     fillers = repeat(fillvalue)
>     iterators = [chain(it, sentinel(), fillers) for it in args]
>     try:
>         while iterators:
>             yield tuple(map(next, iterators))
>     except ZipExhausted:
>         pass
>
> # ========= Option 2
>
> class e(Exception):
>     pass
>
> def zl(*a, **k):
>     f = f.get('fillvalue')
>     c = len(a) - 1
>     def s():
>         nonlocal c
>         if not c:
>             raise e
>         c -= 1
>         yield f
>     ff = repeat(f)
>     i = [chain(i, s(), ff) for i in args]
>     try:
>         while i:
>             yield tuple(map(next, i))
>     except e:
>         pass
>
> # ========
>
> One of them is cribbed straight from the itertools docs. The other is
> the same functionality with shorter variable names. What makes one of
> them better than the other? Answer me that, and I'll continue.


I see you manually 'optimise' the look?
I personally would end with something like this:

def zip_longest(*A, **K):
    value = K.get ('fillvalue')
    count = len(a) - 1
    def sentinel():
        nonlocal count
        if not count:
            raise ZipExhausted
        count -= 1
        yield  value
    fillers = repeat (value)
    iterators = [chain (it, sentinel(), fillers) for it in A]
    try:
        while iterators:
            yield tuple (map (next, iterators))
    except ZipExhausted:
        pass


So I would say, my option would be something inbetween.
Note that I tweaked it for proportional font, namely Times New Roman.

Particularly I find too narrow lines/words a bit eye-straining at times.
Also self-explanation is important in many cases. But that depends of
what context you cut the example.

But if you only ask which code of two looks better for me,
then, probably Second, but it has some issues for me, e.g. "c" and "e"
almost homoglyhs, too loose 'sieve'-like, short lines.


Mikhail



More information about the Python-list mailing list