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…
A Python Guide for the Ages
41–50 of 59 posts
Re: A Python Guide for the Ages
#42Re: A Python Guide for the Ages
#43I'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…
Re: A Python Guide for the Ages
#44"For the ages". Or at least until they release Python 4
https://www.techrepublic.com/article/programming-languages-w...
Re: A Python Guide for the Ages
#45I 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
Re: A Python Guide for the Ages
#46Earlier 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.
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
#47I'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…
Re: A Python Guide for the Ages
#48The 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...
Re: A Python Guide for the Ages
#49There 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 don't think I've seen it in the wild either. Perhaps too new?
Re: A Python Guide for the Ages
#50There 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…
for … :
…
else: # break didn’t occur
…
I typically make it even shorter, but this is probably the most readable.