Python f-strings > Dutch government

Python f-strings > Dutch government

31 comments · 83% upvoted · source · other discussions

comments

[deleted]

Is it pythonic enough?

Upbeat_Spite_9674
b=lambda x:('🔵'*x+'⚪'*100)[:100:10]
sobov

Yeah. Let's make it even more unreadable

Compux72

Yep that’s unreadable

BaalKazar

percentage times ten, min(result, 10)

(x) times result

(0) fill until result gets 10 symbols long

Pretty readable imo, I don’t even know/do python

xaomaw

What about :⚪<10

What does the : do? How can you compare ⚪ to 10?

Arny597

That’s the “fill result until 10 symbols long” part

xaomaw

Okay so I got the answer. The colon : itself stands for an if-statement. That's what my first question was about.

But even then the comparison is

if ⚪ < 10:
    🔵 * count_of_blue_dots

So I still have the question how can you compare ⚪ < 10? I would understand if ⚪ was defined as a variable containing the count_of_white_dots before, but it is NOT.

Arny597

How do you know it’s an if statement when colons have an entirely different function in python as well? This link tells you how colon operators are used for string slicing. So maybe it’s formatting the blue dot to be as many times as the percentage, then filling the string with white dots as long as the string is less than 10 elements long

EDIT: not only that, but when colons ARE used in an if statement, it’s never to replace the words. The ternary operators “?” And “:” which mean “if” and “else” respectively do not exist in python. Shorthand if statements always use the keywords still, such as “a if b else c”

Synedh

It's not an if-statement, it's a format delimiter.

In python, you can format your in-string argument this way : f"Foo {val:format}" where format is a mini-language by itself. It is a very powerful and an efficient way to deal with strings formating.

xaomaw

Thanks for clarification, I'll take a look into it!

Edit:

<   Forces the field to be left-aligned within the available space (this is the default for most objects).

Now I understand it. Very cool

Synedh

Indeed, very useful for unit or date format, if you need your time on two chars, a simple f"{hour:0<2}:{min:0<2}".

Swoop3dp

Basically left-pad but without the drama.

00PT

It looks like one of those obscure syntax elements that is convenient, powerful, and makes sense, but not at all intuitively understood unless you are specifically told how it works or look it up. There seems to be many of these in Python. It's not good for widespread readability.

BaalKazar

Honestly I actually don’t know what the syntax exactly did.

The curly braces with such a comparison made it look like a loop to me. Then I imagined Python doing Python things in which comparing the string against an integer somehow compares the length of the string.

I can imagine automatic refactoring coming up with stuff luck this, I don’t know how alien that syntax is to python bases though, but the case it self is quirky enough

Ithurion2

You can put 5 lines of comment and still be off shorter than the original.

[deleted]

And then you change the code and the comment sits there wrong an rotting by itself.

[deleted]

Would prefer the original any day of the week

Yorick257

We just got a new request from the client. They decided that a step of 10% is too much, they want to reduce it to a 2% step.

[deleted]

Only now it is the time for refactoring.

Koltaia30

I don't like how 10 is written there as a literal 3 times. It should be a function parameter that defaults to 10

BehindThyCamel

My first thought was "Arjan Codes". A bit unfair to the guy, TBH.

[deleted]

Hard to read, performance is probably worse. But hey, it is 🐍. You cannot expect much from a language with disabilities.

[deleted]

language with disabilities

Do you mean the lack of multi-line lambdas?

Or that peculiarity with multable default function arguments?

----

These two things are pretty annoying, but everything else is completely fine in Python.

00PT

There are also various syntax quirks such as ternary expressions having the result be on the left and variable usages coming before the definitions in certain expressions. This makes it less intuitive for people who come from languages other than Python.

[deleted]

OK, ternary syntax is very unusual to me. The whole point of the ternary expression is to be readable while occupying only 1 line.

variable usages coming before the definitions in certain expressions

Could you elaborate please? It seems I missed this one.

This makes it less intuitive for people who come from languages other than Python

Maybe, my acquintance with Ruby made me think that Python is sufficiently reasonable.

In ruby 0 is True, and, AFAIK, there is a different operator precedence for AND and OR.

00PT

An expression such as x+1 for x in list is valid, but I think it would be more intuitive if it was for x in list: x+1 The first has "x" used before the definition later in the expression, while the second switches that.

[deleted]

Ah, this thing is OK for a single line.

But chaining multiple map statements is indeed unreadable :D

EurekaErect

Why are there so many downvotes here lol, this guy are splitting facts

[deleted]

All good, I have an understanding for special people. Power to them.

F0064R

Percentage rounds, the favorite snack of a mathematician.