Live data from Hacker News

A Python Guide for the Ages

gto76.github.io

41–50 of 59 posts

Re: A Python Guide for the Ages

#41

I'm surprised to see mutable default values not mentioned. It's bitten me more than once to discover that: def f(xs = []): xs.append(5) return xs print(f()) print(f()) will print: [5] [5,5] and I love python despite this horrendous decision, but it should be mentioned more often in beginner resources. edit: thanks for the downvote? I've answered on the order of dozens of questions about this very mechanic on SO, via…

Woah TIL. That does seem like a very strange decision.

Re: A Python Guide for the Ages

#43
post #35

I'm surprised to see mutable default values not mentioned. It's bitten me more than once to discover that: def f(xs = []): xs.append(5) return xs print(f()) print(f()) will print: [5] [5,5] and I love python despite this horrendous decision, but it should be mentioned more often in beginner resources. edit: thanks for the downvote? I've answered on the order of dozens of questions about this very mechanic on SO, via…

Default values for keywords are saved as part of the function signature itself, at function definition time. Without this it’s not clear how you’d introspect the default kwarg values of a function? Would they be computed every time the function is inspected? If they are lazy then what if the default value is expensive to compute or has side effects? So I would not say it’s a horrendous decision, it’s a clear trade of…

It might be confusing as it is different from some other languages, such as C++ and JavaScript. They both evaluate default parameter when the function are called.

Re: A Python Guide for the Ages

#45
post #2

I like this. For those of you for whom this list is too terse, check out the excellent LearnXinYminutes site at https://learnxinyminutes.com/docs/python/

Great site. Discussed the other day here: https://news.ycombinator.com/item?id=29518955

To the downvoter(s): yeah I know we shouldn't comment on them, but you made me smile at the gesture today, take it steady. Merry Xmas!

Re: A Python Guide for the Ages

#46
post #35

Earlier quoted context omitted.

Default values for keywords are saved as part of the function signature itself, at function definition time. Without this it’s not clear how you’d introspect the default kwarg values of a function? Would they be computed every time the function is inspected? If they are lazy then what if the default value is expensive to compute or has side effects? So I would not say it’s a horrendous decision, it’s a clear trade of…

It might be confusing as it is different from some other languages, such as C++ and JavaScript. They both evaluate default parameter when the function are called.

The difference is neither of these can introspect a functions arguments at runtime. C++ probably doesn’t need to, as it can do it at compile time with templates, but therein lies the difference.

Not to say that it can’t is not confusing (at least to begin with), but it’s not an oversight. It’s a conscious choice and I think the only safe one you can make.

Re: A Python Guide for the Ages

#47
post #31

I'm surprised to see mutable default values not mentioned. It's bitten me more than once to discover that: def f(xs = []): xs.append(5) return xs print(f()) print(f()) will print: [5] [5,5] and I love python despite this horrendous decision, but it should be mentioned more often in beginner resources. edit: thanks for the downvote? I've answered on the order of dozens of questions about this very mechanic on SO, via…

Don't let it get to you. If you're critical of something, someone will always downvote. On the other hand this behavior means that the default value will only get computed once, so I guess Guido thought it's useful if that value comes e.g. from an expensive function. On the other other hand you could achieve that latter behavior with a lazy function even if the arguments were reevaluated every time you call the funct…

In Common Lisp, default values of optional parameters are evaluated each time (only when the argument is not given of course). They can even reference other arguments that come earlier.

Re: A Python Guide for the Ages

#48
post #6

The best cheatsheet which I have ever seen (besides maybe cheats.rs) is this Python cheatsheet by Laurent Pointal, absolutely outstanding in many ways: https://perso.limsi.fr/pointal/_media/python:cours:mementopy...

It's good, although doesn't mention f-strings, which makes string manipulation in python next-level.

Re: A Python Guide for the Ages

#49

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

Walrus operator.

I don't think I've seen it in the wild either. Perhaps too new?

Re: A Python Guide for the Ages

#50
post #4

There are a couple of language features in Python that I thought were cool when I discovered them, but actually have never ever find a need for them in my code. The first is using an "else" clause in a "for" loop, and the second is returning a value from a generator. Curious whether anybody actually uses either of those.

I actually saw someone use that for-else pattern in advent of code earlier this month. I believe the consensus was that it was definitely the perfect choice for the given use case, but neither he nor I would ever dare use it in production because nobody knows about it and it's super unintuitive. I think the idea is cool (albeit rarely useful), but a different word than "else" for the same functionality would go a lon…

Use with a short comment:

    for … :
        …
    else:  # break didn’t occur
        …
I typically make it even shorter, but this is probably the most readable.
Post reply on HN