TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Python: Common Newbie Mistakes, Part 1

171 点作者 Nurdok将近 12 年前

20 条评论

RyanZAG将近 12 年前
Ouch, that is one extremely non-user friendly feature going on there. The only reason it works at all is because most people use default for strings&#x2F;numbers only.<p>That functionality really should be changed for the next major python release, and have default evaluate each time the function is called - which is how 99.9% of people using it expect it to work.
评论 #6000220 未加载
评论 #6000658 未加载
评论 #6000325 未加载
评论 #6001787 未加载
评论 #6001190 未加载
stormbrew将近 12 年前
The most frustrating thing that bit me when I was first learning python was the error when you pass the wrong number of arguments to a method:<p><pre><code> &gt;&gt;&gt; class A(object): ... def method(self, a): pass ... &gt;&gt;&gt; A().method() Traceback (most recent call last): File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt; TypeError: method() takes exactly 2 arguments (1 given) </code></pre> Once you understand more about how method dispatch works it makes sense, but it&#x27;s really confusing to be told you were doing something you don&#x27;t think you are.
评论 #6001242 未加载
smegel将近 12 年前
Wow I have been using Python for years and didn&#x27;t realize default values were only created when defined. I don&#x27;t think I have ever been bitten by it, but it would have been one helluva bug to track down.
评论 #6000556 未加载
akoumjian将近 12 年前
I&#x27;ve been coding in Python for 8 years now and only this year came up against this behavior. I had a good long laugh after realizing what had just eaten up my previous couple hours.<p>I actually figured it out by printing out the id of the object being modified in my code, and when seeing it was the same had to really scratch my head. Default args are initialized when the function (or similarly, class) is defined, not when executed.
NiceOneBrah将近 12 年前
As a newbie to Python, my biggest issue when coming up to speed on a new project is chasing down the types of arguments to and return values from functions. Previously I was a Java programmer and while I don&#x27;t miss its verbosity, I do miss how I always knew what types I was working with. Does anybody have any tips that make this easier?
评论 #6000863 未加载
评论 #6000873 未加载
评论 #6000756 未加载
nknighthb将近 12 年前
A simple fix to the time&#x2F;now example is also useful for demonstrating first-class functions:<p><pre><code> def print_now(now=time.time): print now()</code></pre>
评论 #6002818 未加载
评论 #6000749 未加载
评论 #6001519 未加载
lightcatcher将近 12 年前
When I saw the title for this post, my first thought was &quot;hmm, mutable default arguments are pretty tricky, hopefully that&#x27;s somewhere on the list&quot;. I wasn&#x27;t disappointed.
tome将近 12 年前
Cool, it looks like the Rachums are Israeli Pythonista brothers:<p><a href="https://news.ycombinator.com/item?id=5998675" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5998675</a>
评论 #6001202 未加载
arms将近 12 年前
I went into this thinking &quot;This&#x27;ll be an obvious example - one I&#x27;m sure I wouldn&#x27;t make.&quot;<p>I was wrong, and now I know better. Thank you.
pyoung将近 12 年前
Ha, that&#x27;s funny. Just ran into this issue a few days ago. Spent an hour or two trying to figure what was going on, thought I was going crazy. Finally caved and asked an experienced python programmer, and they immediately pointed out that lists are mutable. Goes to show the value of working with experienced programmers.
bryanh将近 12 年前
A safe pattern for this without the None check is keyword expansion which will define defaults when a function is called.<p><pre><code> def foo(**kwargs): numbers = kwargs.pop(&#x27;numbers&#x27;, []) numbers.append(9) print numbers</code></pre>
ak217将近 12 年前
I would put the unfortunate scoping design above mutable defaults on the list of things that confused me.<p>Also, I really don&#x27;t like the decision to make Python 2 disobey the system locale and use ascii as the default encoding. Lots of Python programs out there have broken i18n for no good reason because of that (nobody starts teaching python by telling you to use .decode(&#x27;utf-8&#x27;)&#x2F;.encode(&#x27;utf-8&#x27;) when you pipe data, and by the time you realize what&#x27;s wrong, you often already have code out there.)
rhizome31将近 12 年前
Also worth mentioning is the closure gotcha: when you create functions in a loop, the functions&#x27; context points to the context of the latest iteration of the loop. Other languages might have this issue as well and provide alternatives based on map-style constructs. In Python, you can take advantage of the behaviour described by the OP to make your inner functions have their own context, which is initialized when the function gets created.
评论 #6000422 未加载
t1m将近 12 年前
Note that tuples are immutable in python. I usually use tuples as default values for collections:<p>def f(x=()): x = list(x) ...<p>def g(y=(())) y = dict(y) ...<p>f() is the pattern for lists, g() is for dicts. Note that you can pass in regular lists to f() and regular dicts to g().
kevinwuhoo将近 12 年前
I&#x27;ve encountered a similar bug before.<p><pre><code> &gt;&gt;&gt; arrays = [[]] * 5 &gt;&gt;&gt; print arrays ... [[], [], [], [], []] &gt;&gt;&gt; arrays[0].append(1) &gt;&gt;&gt; print arrays ... [[1], [1], [1], [1], [1]]</code></pre>
评论 #6001483 未加载
评论 #6001502 未加载
Siecje将近 12 年前
Why not use parenthesis with print so that it works for python 2 and python 3?
jlas将近 12 年前
Cool. Related: <a href="https://news.ycombinator.com/item?id=5925467" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=5925467</a>
mjhea0将近 12 年前
you could also use a generator function -<p><pre><code> def foo(num=[0]): num[0] = num[0] + 1 return num[0]</code></pre>
评论 #6000295 未加载
sgdnogb2n将近 12 年前
I expected far more from the article. It&#x27;s not like this hasn&#x27;t been blogged about before.
thrownaway2424将近 12 年前
I agree. Python is a common newbie mistake.
评论 #6001380 未加载
评论 #6001191 未加载