6. 模块(一个 .py 文件称为一个模块Module)

import 语句
类似 _xxx 和 __xxx 这样的 函数/变量 是非公开的(private),不应该被直接引用
函数定义: 外部不需要引用的函数全部定义成private,只有外部需要引用的函数才定义为public。
实例属性和类属性
千万不要把实例属性和类属性使用相同的名字,因为相同名称的实例属性将屏蔽掉类属性,但是当你删除实例属性后,再使用相同的名称,访问到的将是类属性。

@property装饰器 --简化代码,避免每个函数编写重复的代码 python 装饰模式--可对函数、方法或类进行装饰
**案例1(不带参数的decorator,两层嵌套):

import functools #导入functools包
def log(func):
@functools.wraps(func) #wrapper.__name__ = func.__name__
def wrapper(*args, **kw): #wrapper可接收任意参数的调用
print 'call %s():' % func.__name__ #在运行 func() 函数前打印一行日志
return func(*args, **kw)
return wrapper @log #等价于执行 now = log(now)
def now(): #调用 now() 将执行在 log() 函数中返回的 wrapper() 函数
print '2013-12-25

**案例2(带参数的decorator,三层嵌套):

import functools
def log(text): #第一层嵌套
def decorator(func): #第二层嵌套
@functools.wraps(func)
def wrapper(*args, **kw): #第三层嵌套
print '%s %s():' % (text, func.__name__) #在运行 func() 函数前打印一行带参数的日志
return func(*args, **kw)
return wrapper
return decorator @log('execute') #等价于执行 now = log('execute')(now)
def now():
print '2013-12-25'

偏函数(Partial function): 结合默认参数理解
--> 设置某些参数的默认值,并返回一个新的函数,之后调用这个新函数会更便于操作。
案例对比:

def int2(x, base=2): #传统方法
return int(x, base)
import functools #采用偏函数方法
int2 = functools.partial(int, base=2) #二进制转换

__future__
案例:在Python 2.7的代码中直接使用Python 3.x的除法,可以通过 __future__ 模块的 division 实现
from __future__ import division

python笔记6:模块的更多相关文章

  1. python笔记25-sys模块

    import sys#sys.argv命令行参数List,第一个元素是程序本身路径# sys.exit('xxxxx')#退出程序,正常退出时exit(0)# print(sys.version) # ...

  2. python笔记6 模块与包 程序开发规范 包 re sys time os模块

    模块与包 python 模块首引用加载到内存,如果再次引用此模块,直接从内存中读取. python文件分为:执行文件(解释器运行的文件),被引用文件(import) 模块引用一共发生了3件事: 1.他 ...

  3. python笔记27-time模块

    import datetime, time#一种是时间戳.一种是格式化时间.一种是时间元组# print(time.timezone) # 和标准时间相差的时间,单位是sprint(int(time. ...

  4. python笔记24-os模块

    import osprint(os.getcwd())#取当前工作目录#os.chmod('/usr/local',7)#给文件目录加权限,7是最高权限print(os.chdir(r"e: ...

  5. 13.python笔记之pyyaml模块

    Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...

  6. Python学习笔记—itertools模块

    这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...

  7. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  8. python笔记之bisect模块

    python笔记之bisect模块 当你决定使用二分搜索时,这个模块会给你带来很大的帮助. 例子 import bisect L = [1,3,3,6,8,12,15] x = 3 #在L中查找x,x ...

  9. python笔记之itertools模块

    python笔记之itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生 ...

  10. python笔记之ZipFile模块

    python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...

随机推荐

  1. (转)Xcode6中自动布局autolayout和sizeclass的使用

    Xcode6中自动布局autolayout和sizeclass的使用   一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的, ...

  2. 802. Find Eventual Safe States

    https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...

  3. loj2049 「HNOI2016」网络

    好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...

  4. Careercup - Microsoft面试题 - 6751316000899072

    2014-05-12 07:10 题目链接 原题: Write a thread safe data structure such that there could be only one write ...

  5. ls 的顺序与倒序排列

    linux 中文件夹的文件按照时间倒序或者升序排列 1,按照时间升序 ls -lrt -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by ...

  6. UVALive 5099 Nubulsa Expo 全局最小割问题

    B - Nubulsa Expo Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  7. CentOS下安装netcat

    CentOS下安装netcat 使用zookeeper过程中,需要监控集群状态.在使用四字命令时(echo conf | nc localhost 2181),报出如下错误:-bash: netcat ...

  8. Ubuntu下建立Pycharm快捷方式

    修改 Exec, Icon 路径,将文件保存 pycharm.desktop,拖到unity侧边栏 [Desktop Entry]Categories=Development;Comment[zh_C ...

  9. jQuery操作DOM基础 - 元素属性的查看与设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 【Luogu】P4219大融合(LCT)

    题目链接 LCTrotate打错尬死 容易发现本题就是问两边子树大小乘积,于是开个数组动态维护LCT每个节点虚子树上有多少点,在Access和Link的时候更新即可. #include<cstd ...