鱼C论坛

 找回密码
 立即注册
查看: 4945|回复: 20

python书写规范

[复制链接]
发表于 2015-8-12 16:54:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大家好  我刚接触python  开始尝试自己写程序 发现python很好用
但是空格和缩进把我整疯了
求大神指点一下  python程序的格式规范是什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-8-12 17:50:22 | 显示全部楼层
''python程序的格式规范''   google第一个就是
http://zh-google-styleguide.readthedocs.org/en/latest/google-python-styleguide/python_style_rules/

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
~风介~ + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-8-12 18:32:24 | 显示全部楼层
进来转转
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-12 22:15:44 | 显示全部楼层
Reed 发表于 2015-8-12 17:50
''python程序的格式规范''   google第一个就是
http://zh-google-styleguide.readthedocs.org/en/latest/g ...

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-13 09:31:05 | 显示全部楼层
哈哈,我也刚学,也没其他语言基础。:lol:缩进对我来我,感觉是天经地义的事:titter:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-13 10:28:19 | 显示全部楼层
进来转转
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-13 10:52:21 | 显示全部楼层
笑脸对世界 发表于 2015-8-13 09:31
哈哈,我也刚学,也没其他语言基础。缩进对我来我,感觉是天经地义的事

我有其他基础  但是有时候缩进明明看着对着呢  但还是错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-13 16:50:20 | 显示全部楼层
weishuaimifeng 发表于 2015-8-13 10:52
我有其他基础  但是有时候缩进明明看着对着呢  但还是错了

tab键可以准确控制缩进,切忌用空格键
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 09:45:36 | 显示全部楼层
这个好,非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 13:31:31 | 显示全部楼层
用IDE有自动排版的格式,Python的缩进就是同一种格式的语句 缩进不能是一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 13:32:04 | 显示全部楼层
zhaoyun369 发表于 2015-8-14 13:31
用IDE有自动排版的格式,Python的缩进就是同一种格式的语句 缩进不能是一样的

是不同格式的语句,缩进不能是一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 14:07:00 | 显示全部楼层
Reed 发表于 2015-8-12 17:50
''python程序的格式规范''   google第一个就是
http://zh-google-styleguide.readthedocs.org/en/latest/g ...

网址收藏了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 17:25:47 | 显示全部楼层
来了解一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 19:17:46 | 显示全部楼层
xiongjingkui 发表于 2015-8-13 16:50
tab键可以准确控制缩进,切忌用空格键


不对,pep8明确规定了用4空格别用tab
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 19:18:53 | 显示全部楼层
4.8. Intermezzo: Coding Style
Now that you are about to write longer, more complex pieces of Python, it is a good time to talk about coding style. Most languages can be written (or more concise, formatted) in different styles; some are more readable than others. Making it easy for others to read your code is always a good idea, and adopting a nice coding style helps tremendously for that.

For Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:

Use 4-space indentation, and no tabs.

4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.

Wrap lines so that they don’t exceed 79 characters.

This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

Use blank lines to separate functions and classes, and larger blocks of code inside functions.

When possible, put comments on a line of their own.

Use docstrings.

Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).

Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.

Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.

Footnotes

[1] Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list).
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-14 19:19:57 | 显示全部楼层
打开IDLE,按F1,看4.8
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2015-8-15 16:58:38 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-16 15:15:59 | 显示全部楼层
是不是缩进呢,我也是刚学python,希望以后能跟您多交流:victory:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-16 22:55:03 | 显示全部楼层
看看,我习惯yongtab
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-17 14:41:56 | 显示全部楼层
笑脸对世界 发表于 2015-8-13 09:31
哈哈,我也刚学,也没其他语言基础。缩进对我来我,感觉是天经地义的事

对于咱们新手,也有点好处,一张白纸要什么写就这么写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-25 13:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表