千里之行

文档状态:编辑中....


人生苦短


暂无评论

Table of Contents

9.23更新

特性


命令行特性


脚本书写


开发环境


IO


模块

输出函数

输入函数

语法


书写

变量

运算

运算符
操作数

控制结构

循环

不值得专门一节写

函数


分类

  1. 有副作用
  2. 无副作用

常见内建函数

参数问题

暂时书写架构
- 默认参数
- 可变参数
- 关键字参数
- etc


使用

方法

做到访问权限控制
1. 特殊方法
__method__()
``

对象

模块


这个是我现在还比较方的东西
1. 每个python脚本文件都可以当做一个模块
2. 模块太大?可以自己

常见

软件工程


语言风格

>>> 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!
>>>

模块结构和布局

结构示意图:

# (1) 起始行(Unix)
        |___[有起始行就能够仅输入脚本名字来执行脚本]
# (2) 模块文档
        |___[简要介绍模块的功能及重要全局变量的含义,
             模块外可通过 module.__doc__ 访问这些内容]
# (3) 模块导入
        |___[导入当前模块的代码需要的所有模块;每个模块仅导入一次(当前模块被加载时);
             函数内部的模块导入代码不会被执行, 除非该函数正在执行]
# (4) 变量定义
        |___[这里定义的变量为全局变量,除非必须,否则就要尽量使用局部变量代替全局变量]
# (5) 类定义
        |___[所有的类都需要在这里定义。当模块被导入时 class 语句会被执行, 类也就会被定义
             类的文档变量是 class.__doc__]
# (6) 函数定义
        |___[此处定义的函数可以通过 module.function()在外部被访问到,当模块被导入时
             def 语句会被执行, 函数也就都会定义好,函数的文档变量是 function.__doc__]
# (7) 主程序
        |___[论这个模块是被别的模块导入还是作为脚本直接执行,都会执行这部分代码
             不会有太多功能性代码,而是根据执行的模式调用不同的函数]
EXAMPLE:hello.py
#!/usr/bin/env python            [1]#$ chmod +x;./hello.py

["document"|'document']                      [2]

import sys                     [3]

debug =true                   [4]

class new(object):            [5]
    pass           

def real():                  [6]
    pass

if __name__ == '__main__':   [7]#模块测试专用,决定了模块的运行导向
    real()

文档

异常控制

  try:
    expression;
  except:
    expression;
  else:
    when try do not has any error;

优化

  1. 尽量将模块变量替换为本地变量,降低询问时间
#如果经常使用 os.linesep,那就替换吧,因为每次使用 1.确认os是模块 2.确认linesep全局变量在os中存在太麻烦了
temp = os.linesep

高级特性


FAQ


  1. 为什么python不需要变量名和变量类型声明?
    2.不太了解元组赋值机制呢?
    3.如何修改python命令提示符?

获取帮助


  1. 使用help(函数名)交互模式使用
  2. 什么是PEP?
    PYTHON增强提案
  3. 查询关键字
    keyword模块
    |___iskeyword()
    |___关键字列表
  4. 查询系统保留字(build_in)
    __builtins__模块
  5. 获得模块文档帮助
    print module.__doc__
  6. 关键字帮助
  7. 工具
    • Debugger --->pdb
    • logger --->logging[five layer log system]
    • Profilers--->profile,hotshot,cProfile//性能测试

更新日志


  1. 9/19添加大量知识框架,属于低级水平知识
  2. 9/20