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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Google's Python style guide

130 点作者 cr4zy大约 13 年前

16 条评论

nikcub大约 13 年前
I don't like Google's import style, and I have noticed it in a lot of their code. They nicely namespace all of their packages and modules only to dump methods and classes into a single namespace when being imported and used.<p>for eg.<p><pre><code> from sound.effects import echo echo.EchoFilter(input, output) </code></pre> What happens is that you then end up importing all of these methods and very quickly you start getting name conflicts. Lets say you want to support a third-party echo function:<p><pre><code> from sound.effects import echo from vendor.soundutil.effects import echo as soundutil_echo </code></pre> You see this all the time in SDK and web API packages. Dozens of modules called 'auth' (which auth? twitter? facebook?) or 'oauth' or 'request'.<p>Lets say you have a user page that integrates with social networks, would you rather:<p><pre><code> from facebook.api.auth import auth from twitter.api.auth import twitter_auth </code></pre> etc. etc. or<p><pre><code> import facebook.api import twitter.api .. facebook.api.auth() twitter.api.auth() </code></pre> You end up either doing 'import as' hacks and a lot of renaming. Code is a lot clearer to read when you see full method names such as facebook.api.auth rather than just 'auth' and 'echo' everywhere. You also don't lose documentation paths.<p>My general rule of thumb is to use 'from' infrequently, never do import *, to retain the part of the path that still keeps namespacing sane and clear to the developer and as the doc says to never do relative imports.<p>It means you can scan any part of the code and understand what is going on without going back up to the top of the file. Also makes search/replace easier (rather than s/echo/echo_new s/sound.effects.echo/mynewpackage.echo)<p>The other one I didn't see mentioned is nesting levels and method lengths. Python isn't well suited to deep-nested and long methods. Especially if your coding style is to comment out blocks of code during development as you test things, you always end up commenting out parts and then having to re-indent the rest of it.<p>The same usually applies if you have long 'and' 'or' clauses in ifs that span multiple lines and make it harder to understand the code. I usually wrap those tests into separate methods (if you are using them once, you will probably use them again)<p>but for nesting, I try to stick to 2 levels max. If you go beyond that it is usually a hint that you can refactor the codepath and perhaps even separate out into another method.<p>I just happen to be doing this a few hours ago while writing an option and argument parser for a command line utility that has sub-commands. a quick re-factor made the code and all the different options and which options apply to which sub-commands a lot easier to understand<p>Edit: just further on breaking up code and bounds checking into methods, it makes life easier for other developers and for your future self. there is nothing more exhausting than trying to debug a module and finding a 3-page long method called 'run', which you end up having to break down yourself anyway. separate all the bounds checking into one or two line methods, break everything else up, document it, write some tests for it and then forget about it - that is done and it works. get on with important things.<p>checking nesting levels and method length is almost something I would want to put in a linter
评论 #3862698 未加载
评论 #3862595 未加载
scorpion032大约 13 年前
"List Comprehensions: Ok to use for simple cases (only)": <a href="http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#List_Comprehensions" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...</a><p>Really, Google? I find the following far more convenient to read:<p><pre><code> result = [(x, y) for x in range(10) for y in range(5) if x * y &#62; 10] </code></pre> Than the alternative:<p><pre><code> result = [] for x in range(10): for y in range(5): if x * y &#62; 10: result.append((x, y))</code></pre>
评论 #3862723 未加载
评论 #3863188 未加载
评论 #3863285 未加载
评论 #3863840 未加载
qznc大约 13 年前
I like the idea to include a name with TODOs, like<p><pre><code> # TODO(qznc) check Unicode handling </code></pre> This never occured to me, but it provides a good pointer whom to ask for details. On the other hand git-blame should be able to provide the same info.
评论 #3861827 未加载
评论 #3861983 未加载
评论 #3861903 未加载
评论 #3863813 未加载
评论 #3862117 未加载
waleedka大约 13 年前
Interestingly, the Python code I've seen from Google uses 2-space indents rather than 4 as the style guide recommends. And that includes code written by Guido himself (AppStats and NDB, tools used in App Engine). I prefer 2 spaces as well, and I was hoping that the official style guide would match what's being used most commonly.
评论 #3861914 未加载
评论 #3861888 未加载
评论 #3866345 未加载
评论 #3862197 未加载
jdevera大约 13 年前
Why oh why the 80 character limit? It's the 21st century, screens are huge! I'm not saying let's put the limit in 300, but 100 or 120 is good enough to fit side by side diffs in one screen.
评论 #3862387 未加载
评论 #3862571 未加载
评论 #3863116 未加载
评论 #3866352 未加载
jbarham大约 13 年前
Much as I like the general consistency of Python code with or without formal style guides, I prefer Go's "style guide" even more, which is to just run "go fmt": <a href="http://golang.org/cmd/go/#Run_gofmt_on_package_sources" rel="nofollow">http://golang.org/cmd/go/#Run_gofmt_on_package_sources</a>.
评论 #3862369 未加载
yuvadam大约 13 年前
Weird. The recommendation for a shebang line is to use #!/usr/bin/python<p>That will definitely break stuff. Why not #!/usr/bin/env python?
评论 #3861792 未加载
评论 #3862074 未加载
shawnps大约 13 年前
Regarding this one:<p><a href="http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Default_Argument_Values#Default_Argument_Values" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...</a><p>I commented about this on another post. There's a nice explanation here as to why using mutable objects as default values in function/method definitions is bad:<p><a href="http://effbot.org/zone/default-values.htm" rel="nofollow">http://effbot.org/zone/default-values.htm</a><p>In short, it can be bad to set default arguments to mutable objects because the function keeps using the same object in each call.
评论 #3862290 未加载
iloveponies大约 13 年前
Interesting they make no reference to PEP 8.
评论 #3861730 未加载
评论 #3862374 未加载
plq大约 13 年前
&#62; Never use catch-all except: statements, or catch Exception or StandardError, unless you are re-raising the exception or in the outermost block in your thread (and printing an error message). Python is very tolerant in this regard and except: will really catch everything including Python syntax errors. It is easy to hide real bugs using except:.<p>What kind of SyntaxErrors are cought by the except: handler? Not all, I presume:<p><pre><code> try: a b except: pass </code></pre> This fails with SyntaxError on Python 2.7.2 on my machine.
评论 #3861883 未加载
评论 #3861927 未加载
maybe_someday大约 13 年前
I wonder why pyChecker and not pyLint or pep8. Anyone got an insight?
评论 #3862168 未加载
willvarfar大约 13 年前
Fun fact: Google recommend <i>against</i> using map/reduce :)<p><a href="http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Deprecated_Language_Features#Deprecated_Language_Features" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...</a><p>Oh, not different map/reduce? ;)
评论 #3864952 未加载
droctopu5大约 13 年前
Wait, I thought Python WAS a style guide.
lclarkmichalek大约 13 年前
The last point is definitely the most important, and applies to any language
silon4大约 13 年前
Personally, in my own code I also like to put pass at the end of all blocks. It's more consistent, and it also makes auto indent in the editors work properly. Anyone else?
danso大约 13 年前
For an offhand comparison: Google's unofficial Ruby style guide:<p><a href="http://www.caliban.org/ruby/rubyguide.shtml" rel="nofollow">http://www.caliban.org/ruby/rubyguide.shtml</a>