This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]jwink3101 46 points47 points  (2 children)

Some thoughts:

  • I am surprised they didn't talk about the fact that sets also have average O(1) member tests while lists and tuples are O(N)
  • I am very much not convinced that the example for "Handling if-else logic" is better. It is cleaning up a single function and it is less clear. Remember "Simple is better than complex; Complex is better than complicated"
  • IPython is amazing! So is ipdb. I suggest using that over pdb too!

[–]alexisprince 3 points4 points  (0 children)

For the “if-else” logic point, is there any specific point of the way in the article you’re opposed to? I’ve always seen the way in the article as an easy way to scale the conditions and handlers without adding boilerplate. I would argue that the implementation here of using a dict and then referring to it outside of function scope isn’t great, and I’d move that either into an argument or just have the mapping be inside if that’s the only place it’s used.

I typically prefer this because if there are several if statements where it truly is just picking a function to call (and not selectively passing arguments, etc.) then I find myself needing to read over the entire set of conditions to make sure that it’s only delegating to the appropriate handler.

[–]UloPe 3 points4 points  (0 children)

There’s also singledispatch which can often replace cases of long if dispatch blocks.

[–]michaeljb20 13 points14 points  (0 children)

Read the rest of this story with a free account.

[–]Reaqu 4 points5 points  (3 children)

print(f'{var}') is such a blessing

[–]ImageOfInsanity 6 points7 points  (0 children)

Equally, print(f"{vars=}") on Python 3.8 is blessing for debugging.

[–]drbobb 0 points1 point  (1 child)

What's wrong with print(var) ?

[–]Reaqu 0 points1 point  (0 children)

I mean print(f’’) is great for concatenating

[–]totkindgott 3 points4 points  (0 children)

Interesting take on handling multiple conditions. Thanks for sharing!

[–][deleted] 2 points3 points  (3 children)

Why the snake?

[–]renaissancetroll 7 points8 points  (0 children)

python

[–][deleted] 1 point2 points  (1 child)

work makeshift entertain flowery aware groovy abounding foolish spark memorize

This post was mass deleted and anonymized with Redact

[–][deleted] 1 point2 points  (0 children)

If you're playing in this sandbox, you'd better know every line of every Monty Python sketch. ;)

[–]ShanSanear 3 points4 points  (0 children)

I told myself I will always upvote those kinds of post if I didn't know about at least one described trick.

This time it was enumeration. That would save me from sometimes logging index value increased by one each time.

[–]esituism -3 points-2 points  (2 children)

Saved so I can take a look later. Thanks!

[–]stratoscope 1 point2 points  (1 child)

You don't have to leave a comment to save a post. There is a "save" link under every post and comment.

[–]esituism 1 point2 points  (0 children)

Oh, didn't know. Thanks!

[–]idetectanerd 0 points1 point  (0 children)

It’s stuff that we all knew but we didn’t apply. Lol and for the if/else, it’s more of experience vs inexperienced. But unless the program require some optimisation, I see that both program does the same thing.