Core Python Programming(1) - Basic

core python programming 核心编程-读书笔记1

语句中有一些基本规则和特殊字符:
1 井号(#)表示之后的字符为 注释
2 换行 (\n) 是标准的行分隔符(通常一个语句一行)
3 反斜线 ( \ ) 继续上一行
4 分号 ( ; )将两个语句连接在一行中
5 冒号 ( : ) 将代码块的头和体分开
6 语句(代码块)用缩进块的方式体现
7 不同的缩进深度分隔不同的代码块
8 文件以模块的形式组织

标识符字符串规则和其他大部分用C 编写的高级语言相似:
第一个字符必须是字母或下划线(_)
剩下的字符可以是字母和数字或下划线
大小写敏感

渐渐的,你会听到“Pythonic”这个术语,它指的是以 的方式去编写代码、组织
逻辑,及对象行为。更多时间过后,你才会真正理解它的含义。PEP 20 写的是 之禅,
你可以从那里开始你探索“Pythonic”真正含义的旅程。如果你不能上网,但想看到这篇诗句,
那就从你的 解释器输入 import this 然后回车(见下面)。下面是一些网上资源:
www..org/doc/essays/styleguide.html
www..org/dev/peps/pep-0007/
www..org/dev/peps/pep-0008/
www..org/dev/peps/pep-0020/
www.Python.org/dev/peps/pep-0257/

>>> import this
The Zen of , 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!

核心笔记:__name__ 指示模块应如何被加载
由于主程序代码无论模块是被导入还是被直接执行都会运行, 我们必须知道模块如何决定
运行方向。一个应用程序可能需要导入另一个应用程序的一个模块,以便重用一些有用的代码
(否则就只能用拷贝粘贴那种非面向对象的愚蠢手段)。这种情况下,你只想访问那些位于其
它应用程序中的代码,而不是想运行那个应用程序。因此一个问题出现了,“ 是否有
一种方法能在运行时检测该模块是被导入还是被直接执行呢?” 答案就是……(鼓声雷
动)…..没错! __name__ 系统变量就是正确答案。
如果模块是被导入, __name__ 的值为模块名字
如果模块是被直接执行, __name__ 的值为 ‘__main__’

 

核心技巧:使用局部变量替换模块变量
类似 os.linesep 这样的名字需要解释器做两次查询:(1)查找os 以确认它是一个模块,
(2)在这个模块中查找 linesep 变量。因为模块也是全局变量, 我们多消耗了系统资源。如
果你在一个函数中类似这样频繁使用一个属性,我们建议你为该属性取一个本地变量别名。 变
量查找速度将会快很多--在查找全局变量之前, 总是先查找本地变量。 这也是一个让你的
程序跑的更快的技巧: 将经常用到的模块属性替换为一个本地引用。代码跑得更快,而也不用
老是敲那么长的变量名了。在我们的代码片段中,并没有定义函数,所以不能给你定义本地别
名的示例。不过我们有一个全局别名,至少也减少了一次名字查询

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. Python Programming – Sqlite for data persistence
  2. Python programming- List extend() and append()
  3. Python 3 简介
  4. Python HTML Parser Performance
  5. Python 3.1.1 RC发布
  6. Eclipse SDK + PyDev = Python IDE
  7. Book: Cross-Platform GUI Programming with wxWidgets (Bruce Perens)(含中英文版本下载地址)
  8. A rough guide for wxWidgets programming
  9. Python用SGMLParser抓取网页连接的改进
  10. 开始Python — Dictionary

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu