这个问题好像有点蛋疼,从c++转到python,总感觉写出来的代码一点都没有python的风格。看上去就想是把c++硬生生给翻译成了python的语言。是因为c++写惯了的缘故吗?还是个人原因,或者有谁能告诉下怎么样可以写出更python风格的python代码T T
1
snip Aug 31, 2013
我也对这个问题有兴趣,同问
|
2
eatmore Aug 31, 2013
1) 随便找个知名的开源python项目看看
2) 读那本cookbook |
3
lisztli Aug 31, 2013
问问题的时候,把"python风格" 改成 "pythonic"
|
5
meta Aug 31, 2013
我觉得多用python的特性和方言就比较像了,比如slice或者list的遍历操作什么的。
|
6
tioover Aug 31, 2013 贴一点你认为不够pythonic 的代码呗
|
7
ThunderEX Aug 31, 2013 |
8
ipconfiger Aug 31, 2013
去玩玩一行实现XXX什么的,就可以把各类奇技淫巧都掌握了。但是那个并不是Pythonic,Pythonic就是怎么简单怎么来
|
9
no13bus Aug 31, 2013
|
10
zorceta Aug 31, 2013 PEP8
|
11
G6tFI41JufeV7M9i Aug 31, 2013 如果你只会python 写出来的绝对是python风格。
|
12
pythoner Aug 31, 2013
1,先理解 The Zen of Python
2,阅读优秀的开源python项目源码 3,看看pep8,并竟可能的遵循它 |
13
ushuz Sep 1, 2013
import this & pep8
|
14
kid177 OP |
15
liushuaikobe Sep 1, 2013
>>> import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> |
16
unionx Sep 1, 2013
有人写的Python是Lisp风格,有人写的Python是Java风格
我还真没见过Python风格的 |
17
metaclass Sep 2, 2013
其实著名的Python项目有些也是用别的风格写的
比如Django,会有这样的写法: context = { 'protocol': use_https and 'https' or 'http', } 其中use_https是一个bool 传统Python风格的写法应该是: context = { 'protocol': 'https' if use_https else 'http', } 不过第一种写法由于和a ? b : c差不多,条件放最前面两个赋值放最后面可能会更容易识别一些,用那个写法的还不少 |
19
raptor Sep 2, 2013
没事,只要能跑就行了,看得多写得多以后自然就pythonic了,不要急
|
20
thinkhu Sep 2, 2013
v2ex是python写的吧,有时候发布主题出错!
|