(1)命名惯例
以单一下划线开头的变量名(_X)不会被from module import*等语句导入
前后有两个下划线的变量名(__X__)是系统定义的变量名,对解释器有特殊意义
以两个下划线开头但不以下划线结尾的变量名(__X)是类的本地(私有)变量
(2)文档字符串:同样需要缩进
"""
module document
"""
def func():
"""
function document
"""
print()
class Employee(object):
"""
class document
"""
print()
print(func.__doc__) # 输出函数文档字符串
print(Employee.__doc__) # 输出类的文档字符串
(3)函数相关的语句和表达式
myfunc('spam') # 函数调用
def myfunc(): # 函数定义
return None # 函数返回值
global a # 全局变量
nonlocal x # 在函数或其他作用域中使用外层(非全局)变量
yield x # 生成器函数返回
lambda # 匿名函数
(4)函数的参数
a.函数的形参和实参,定义在函数体内的参数是形参,调用时候传入参数是实参
def print_name(name):
print('your name is %s' % name)
print_name('heygor')
#例子中形参为定义函数时候括号中的name,实参是调用函数时候传入的heygor
b.位置参数:函数允许定义多个形参,也可能包含多个实参,通过形参和实参对应顺序就是位置实参,只有位置一致才能匹配
def animal(pet1,pet2):
print(pet1+'wang!!!'+pet2+'miao~~~')
animal('cat','dog')
c.关键字参数,关键字传递函数名称-值对,直接在实参中把名称和值关联起来,就不会混淆
def animal(pet1,pet2):
print(pet1+'wang!!!'+pet2+'miao~~~')
animal(pet2='mimi',pet1='erha')
d.默认参数,函数定义时候设置函数是形参,每一个形参设置一个默认值,当函数调用时候,没有实参,就是形参默认值
def animal(pet2,pet1='erha'):
print(pet1+'wang!!!'+pet2+'miao~~~')
animal('bosi')
animal('pig','out man')
e.不定长参数
有些时候一个函数处理比当初声明更多的参数叫做不定长参数
*args位置参数传入装配成元组类型
**args位置参数传入装配成字典类型
def test(x,y,*args):# *args
print(x,y,args)
test(1,2,'heygor','ladeng')
def test1(x,y,**args):# **args
print(x,y,args)
test1(1,2,a=6,b=7,c='heygor')
1.寻求帮助
1.1dir()简单的列出对象obj所包含的方法名称,返回一个字符串列表
1.2help()详细列出对象obj的介绍及用法
>>> import re#查询的模块/方法,需要先导入模块
>>> dir(re)
['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
>>> help(re)
#进入re模块的帮助文档,并无返回值,按q退出文档
>>>
1.3print()函数原型,流的重定向
print语法:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
实例:
>>> print('abcdefghijklmn')
abcdefghijklmn
>>> print('abcdefghijklmn',end='')
abcdefghijklmn>>>
#流的重定向
>>> import sys #导入sys模块
>>> temp=sys.stdout #保存原始流
>>> sys.stdout=open('log.log','r+') #长重定向方向到log.log文件
>>> print('hello world') #打印'hello world',可见终端不显示
>>> sys.stdout.close() #关闭文件
>>> sys.stdout=temp #还原流
>>> print('hello world') #打印,终端显示,等于sys.stdout.write('hello world')
hello world
1.4python变量名解析:LEGN原则
local(functin) --> encloseing function locals --> global(module) --> build-in(python)
说明:以下边的函数maker为例 则相对于action而言 X为Local N为Encloseing
嵌套函数举例:工厂函数
def maker(N):
def action(X):
return X**N
return action
f=maker(3) #给N传参
print(f(3)) #给X传参
15.嵌套函数
#-- 嵌套函数举例:工厂函数
def maker(N):
def action(X):
return X ** N
return action
f = maker(2) # pass 2 to N
f(3) # 9, pass 3 to X
#-- 嵌套函数举例:lambda实例
def maker(N):
action = (lambda X: X**N)
return action
f = maker(2) # pass 2 to N
f(3) # 9, pass 3 to X
16.nonlocal和global语句的区别
nonlocal应用于一个嵌套的函数的作用域中的一个名称
global应用于全局的变量,即def之外的变量
# nonlocal应用于一个嵌套的函数的作用域中的一个名称 例如:
start = 100
def tester(start):
def nested(label):
nonlocal start # 指定start为tester函数内的local变量 而不是global变量start
print(label, start)
start += 3
return nested
t=tester(2)
t(10)
# global为全局的变量 即def之外的变量
def tester(start):
def nested(label):
global start # 指定start为global变量start
print(label, start)
start += 3
return nested
t=tester(2)
t(10)
17.函数参数:不可变参数(位置,默认,关键字参数)通过值传递,可变参数(不定长参数)通过引用传递
位置参数
关键字参数
默认参数
#-- 函数参数,不可变参数通过“值”传递,可变参数通过“引用”传递
def f(a, b, c): print(a, b, c)
f(1, 2, 3) # 参数位置匹配
f(1, c = 3, b = 2) # 参数关键字匹配
def f(a, b=1, c=2): print(a, b, c)
f(1) # 默认参数匹配
f(1, 2) # 默认参数匹配
f(a = 1, c = 3) # 关键字参数和默认参数的混合
# Keyword-Only参数:出现在*args之后 必须用关键字进行匹配
def keyOnly(a, *b, c): print('') # c就为keyword-only匹配 必须使用关键字c = value匹配
def keyOnly(a, *, b, c): ...... # b c为keyword-only匹配 必须使用关键字匹配
def keyOnly(a, *, b = 1): ...... # b有默认值 或者省略 或者使用关键字参数b = value
可变参数(不定长参数)匹配:*和**
#-- 可变参数匹配: * 和 **
def f(*args): print(args) # 在元组中收集不匹配的位置参数
f(1, 2, 3) # 输出(1, 2, 3)
def f(**args): print(args) # 在字典中收集不匹配的关键字参数
f(a = 1, b = 2) # 输出{'a':1, 'b':2}
def f(a, *b, **c): print(a, b, c) # 两者混合使用
f(1, 2, 3, x=4, y=5) # 输出1, (2, 3), {'x':4, 'y':5}
#-- 函数调用时的参数解包: * 和 ** 分别解包元组和字典
func(1, *(2, 3)) <==> func(1, 2, 3)
fu