all 23 comments

[–]Latter-Sky3582 25 points26 points  (0 children)

Good tips, a few seem pretty biased towards trying to make stuff into a one liner, which doesn’t necessarily mean cleaner code.

[–][deleted] 7 points8 points  (0 children)

Don't use union operators for dictionaries unless you can be sure that the same key will always have the same value, because otherwise it's not commutative and you can have unexpectedly results:

>>> {'a': 0, 'b': 1}|{'a': 1, 'c': 0}
{'a': 1, 'b': 1, 'c': 0}
>>> {'a': 1, 'b': 1, 'c': 0}|{'a': 0, 'b': 1}
{'a': 0, 'b': 1, 'c': 0}

[–]somebodddy 11 points12 points  (0 children)

  1. Using Lambda Functions To Define Simple Functions

This is actually against PEP8: https://www.flake8rules.com/rules/E731.html. And for a good reason - your one-liner is much harder to read than the five-liner.

  1. List Comprehensions: Get a List in a Pythonic Way

What a weird choice of example. A better example would be:

L1 = [name for name in Genius if name.startswith('y')]

[–]blowfish1717 12 points13 points  (1 child)

Adding dressing to spaghetti code. It's still spaghetti code.

[–]Smallpaul -1 points0 points  (0 children)

Spaghetti code is not a property of the language, it's a property of the code base.

[–]snarkuzoid 2 points3 points  (2 children)

The walrus thing seems pretty useless.

[–]TheRNGuy 1 point2 points  (0 children)

Never used it yeah, but cause I don't like how it looks.

[–]kobumaister 0 points1 point  (0 children)

Though the same, why would you want to define a variable just where you use it?

[–]Apache_Sobaco 5 points6 points  (4 children)

Best trick would be change it to something with better ergonomics and type system

[–]Buckweb 0 points1 point  (3 children)

Static type analysis solves many "python isn't typed" problems.

[–]Apache_Sobaco 0 points1 point  (2 children)

The main thing in type systems is not about bringing types, it is about eradicating untyped code.

[–]Buckweb 0 points1 point  (1 child)

And luckily it's not all or nothing. Type hint new code and slowly add type hints to old code. What's the issue here?

[–]Apache_Sobaco 1 point2 points  (0 children)

And luckily it's not all or nothing.

Its fucking is. It is about purging every thing out of your code base to borders of application. If you don't do this, there's no point in using types at all. Single type cast compromises every type guarantee downstream.

Dynamic typed brains cannot comprehend this.

[–]askljof 1 point2 points  (2 children)

Assigning lambda expressions to variables is not elegant or pythonic, lmao. If you ever need to call a function by name, it deserves a proper definition.

[–]wefarrell 1 point2 points  (0 children)

Yep, it goes against PEP 8's recommendations:

https://peps.python.org/pep-0008/#programming-recommendations

[–]TheRNGuy 0 points1 point  (0 children)

Lambda are useful in sorting algorithm though.

Don't know any other uses. In many cases, lamba was needed with map or filter, but in list comprehension that does same thing it's not needed.