<PY><core python programming笔记>C3 Python基础
2014-04-28 21:10
731 查看
C3 Python基础
# 注释\ 继续
: 代码组 不同代码组由不同的缩进分隔
; 续行
当一个模块变得过大 并且驱动了太多功能的话,就应该考虑拆一些代码出来另外建一个模块
跨平台的话建议不要使用制表符来缩进 直接空格
3.2变量赋值
赋值操作符 = 传了一个引用 可以链式赋值即多重赋值
增了赋值 += -= *= /= %= **= <<= >>= &= ^= |= (不支持自增 ++ --)
多元赋值 x,y,z=1,2,'a string' (x,y,z)=(1,2,'a string')
3.3标示符
合法的标示符
关键字
and as assert break class continue def del
elif else except exec finally for from global
if import in is lambda not or pass print raise return
try while with yield None
内建
python 用下划线作为变量的前缀和后缀指定特殊变量
_xxx 不用从from module import * 导入
_xxx_ 系统定义名字
_xxx 类中的私有变量名
#建议仅在类内使用下划线开头的变量名 作为私有
#避免随意使用双下划线
3.4基本编程风格
文档
声明中第一个没有赋值的字符串可以通过 .__doc__访问 #双下划线_ _
缩进
不建议tab(需要多处使用时) 建议用4个空格
选择好的标示符名称
3.4.1模块结构和布局
# /usr/bin/env python #指定执行 unix需要
# coding=utf-8 #指定编码
"this is a test module" #模块文档
import sys #模块导入 每个模块导入一次
debug=True #全局变量定义 尽量使用局部变量
class FooClass(obj): #类定义(若有) 写__doc__
"Foo Class"
pass
def test():
"test function" #函数定义(若有)
pass
if __name__=='__main__':#主程序 检查__name__ 绝大部分模块是为了被别人调用
#main中就可以直接做该模块的测试工作了
test()
#最安全的做法是所有代码都在函数中
3.5内存管理
动态类型 变量名即变量类型无需先声明 自动更具右侧判断
内存释放由解释器去做
引用计数的方式 多一次引用就+1 删除或重新赋值就-1 变0就回收
del可以删除对象的一个引用
相关文章推荐
- <PY><core python programming笔记>C5 数字
- <PY><core python programming笔记>C11 函数和函数式编程
- <PY><core python programming笔记>C21 数据库编程
- <PY><core python programming笔记>C2 快速入门
- <PY><core python programming笔记>C12 模块
- <PY><core python programming笔记>C14 执行环境
- <PY><core python programming笔记>C17 网络客户端编程
- <PY><core python programming笔记>C9 文件和输入输出
- <PY><core python programming笔记>C6 序列:字符串和列表和元组(二)
- <PY><core python programming笔记>C10 错误和异常
- <PY><core python programming笔记>C8 条件与循环
- <PY><core python programming笔记>C7 字典和集合类型
- <PY><core python programming笔记>C18 多线程编程
- <PY><core python programming笔记>C19 图形用户界面编程
- <PY><core python programming笔记>C4 Python对象
- <PY><core python programming笔记>C13 面向对象编程
- <PY><core python programming笔记>C6 序列:字符串和列表和元组(三)
- <Test-Driven Development with Python>学习笔记 第一部分 测试驱动开发基础
- <PY><core python programming笔记>C16 网络编程
- <<Python基础教程>>学习笔记 | 第04章 | 字典