Python开发基础 day5 函数的定义与使用
#coding:utf-8:
# x='你' # u'hello'
# print(x.encode('gbk'))
# print(b'\xc4\xe3')
# y=b'\xc4\xe3'
# print(y.decode('gbk'))
# x='你' #unicode.encode('utf-8')
y=u'你'
print repr(y)
# while True:
# msg=input('>>: ')
#
# # print(len(msg))
# # res=len(msg)
# # if res < 5:
# # continue
# # else:break
#
#
# if len(msg) < 5:
# continue
# else:break
#
# with open('a.txt') as f:
# for i in f.readlines():
# print(i,end='')
# with open('a.txt') as f:
# for i in f:
# print(i,end='')
# with open('a.txt') as f:
# print(f.readline(),end='')
# print(f.readline(),end='')
# print(f.readline(),end='')
# print(f.readline(),end='')
# print(f.readline(), end='')
# print(f.readline(), end='')
# with open('a.txt','r',encoding='utf-8') as f:
# print(len(f.read()))
# with open('a.txt','rb') as f:
# file_size=len(f.read())
# import os
# print(os.path.getsize('a.txt'))
函数参数的使用:
# def foo(x):
# print(x)
#
#
# foo(1)
# foo('a')
# foo({'a':2})
#形参与实参:
# def foo(x,y): #x=1,y=2
# return x+y
#
# foo(1,2)
#===================================part1
#位置参数:按照从左到右的顺序依次定义的参数
def foo(x,y):
print(x)
print(y)
#按位置定义的形参,必须被传值,多一个不行,少一个也不行
# foo(1,2,3)
#按位置定义的实参,与形参一一对应
# foo(2,10)
#===================================part2
#关键字参数:实参在定义时,按照key=value形式定义
# def foo(x,y):
# print(x)
# print(y)
#
# # foo(y=10,x=1)
# foo(y=10,x=1) #关键字参数可以不用像位置实参一样与形参一一对应,指名道姓地传值
def foo(x,y):
print(x)
print(y)
# foo(1,10)
# foo(x=1,y=10)
# foo(1,y=10)
# foo(y=10,1)
# foo(x=10,1)
# foo(1,x=10,y=20)
# def foo(x,y,z):
# print(x)
# print(y)
# print(z)
# # foo(1,z=20,10)
# foo(1,y=2,z=10)
#注意的问题一:位置实参必须在关键字实参的前面
#注意的问题二:实参的形式既可以用位置实参又可以是关键字实参,但是一个形参不能重复传值
#===================================part3
#默认参数:在定义函数阶段,就已经为形参赋值,定义阶段有值,调用阶段可以不用传值
# def func(x,y=10):
# print(x)
# print(y)
# func(1,20)
# func(1)
# def func(y=10,x):
# print(x)
# print(y)
#位置实参
#关键字实参
#位置形参
#默认参数(形参)
#形参的应用:值经常变化的需要定义成位置形参,
# 值大多数情况下都一样,需要定义成默认参数
# def register(name,age,sex='male'):
# print(name,age,sex)
#
# register('alex',1000)
# register('wupeiqi',9000)
# register('yuanhao',10000)
# register('王铁蛋',10000,'female')
#默认参数需要注意的问题一:必须放在位置形参后面
#默认参数需要注意的问题二:默认参数通常要定义成不可变类型
#默认参数需要注意的问题三:默认参数只在定义阶段被赋值一次
# x=10
# def func(name,age=x):
# print(name)
# print(age)
# x=20
#
# func('egon')
# def func(name,age=10,sex='male',hobby=['play1','play2']):
# print(name)
# print(age)
# print(hobby)
#
# func('egon')
#===================================part4
#可变长参数:可变长指的是实参的个数不固定
#按位置定义的可变长度的实参:*
#按关键字定义的可变长度的实参:**
#
# def func(x,y,*args): #x=1,y=2,args=(3,4,5,6)
# print(x,y)
# print(args)
#
# func(1,2,3,4,5,6)
# def func(x,y,*args): #args=(3,4,5,6)
# print(x,y)
# print(args)
#
# func(1,2,*(3,4,5,6)) #foo(1,2,3,4,5,6)
# def func(x,y,z):
# print(x,y,z)
#
# # func(1,*(2,3)) #func(1,2,3)
# func(*(2,3)) #func(2,3)
#
# def func(x,y=2,z=1):
# print(x,y,z)
#
#
# func(*('a','b','c')) #func('a','b','c')
# def func(x,y,**kwargs): #x=1,y=2,kwargs={'a':1,'b':3,'z':3}
# print(x,y)
# print(kwargs)
#
# func(1,y=2,z=3,a=1,b=3)
# def func(x,y,**kwargs): #x=1,y=2,**kwargs=**{'a':1,'b':3,'z':3}
# print(x,y)
# print(kwargs)
#
# func(1,y=2,**{'a':1,'b':3,'z':3}) #func(1,y=2,z=3,b=3,a=1)
# def func(x,y=1,z=1):
# print(x,y,z)
#
# func(**{'y':2,'x':1,'z':3}) #
# def wrapper(*args,**kwargs): #可以接受任意形式,任意长度的参数
# print(args)
# print(kwargs)
#
#
# wrapper(1,2,3,3,3,3,3,x=1,y=2,z=3)
#
# def index(name,group):
# print('welcome %s to index page,group is: %s' %(name,group))
#
# def wrapper(*args,**kwargs): #可以接受任意形式,任意长度的参数
# # print(args) #args=(1,2,3)
# # print(kwargs) #kwargs={'x':1,'y':2,'z':3}
# index(*args,**kwargs) #index(*(1,2,3),**{'x':1,'y':2,'z':3}) #index(1,2,3,z=3,y=2,x=1)
#
# wrapper(name='egon',group='group1')
#命名关键字参数:定义在*后的形参,这类形参,必须被传值,而且要求实参必须是以关键字的形式来传值
# def register(**kwargs):
# print(kwargs)
# if 'name' in kwargs:
# print(kwargs['name'])
# if 'age' in kwargs:
# print(kwargs['age'])
#
#
# # register(name='egon',age=18)
# register()
# def register(*args,name='egon',age):
# print(args)
# print(name)
# print(age)
#
# # register(name='egon',age=18)
# register(1,2,2,3,age=10)
#
# def register(name,age,*,group,**kwargs):
# print(name)
# print(age)
# print(kwargs)
#
#
# # register('egon',18)
# register('egon1',18,hobby='paly1',group='group1')
# def func(x,y=1,*args,z,**kwargs):
# print(x)
# print(y)
# print(args)
# print(z)
# print(kwargs)
#
# func(1,2,3,4,5,z=10,a=1,b=2)
#
# def func(x,*args,z=1,**kwargs):
# print(x)
# print(args)
# print(z)
# print(kwargs)
#
# func(1,2,3,4,5,a=1,b=2,c=3)
#形参:位置形参,默认参数,*args,命名关键字参数,**kwargs
函数的返回值:
# y=x**2
# def func(x):
# return x**2
# y=func(10) # 10**2
#
# print(y)
# def foo():
# return None
#
# res=foo()
# print(res)
# def foo():
# return {'a':1}
#
# res=foo()
# print(res['a'])
# def foo():
# return {'a':1},1,'a',[1,2]
#
# res=foo()
# print(res)
'''
返回值:可以返回任意类型
没有return:None
return value: value
return val1,val2,val3 :(val1,val2,val3)
return的效果:只能返回一次值,终止函数的执行
'''
def foo():
return 1
print('=====>')
return 2
return 3
return 4
print(foo())
函数的使用原则:
#函数的使用必须遵循:先定义后使用的原则
#函数的定义,与变量的定义是相似的,如果没有事先定义函数而直接引用
#就相当于在引用一个不存在变量名
# print(x) #NameError
# x=1
# func
#
# def func():
# print('hello world')
# def func():
# print('hello world')
#
# # print(func)
# func()
# def bar():
# print('from bar')
#
# def foo():
# print('from foo')
# bar()
#
# foo()
# #定义阶段
# def foo():
# print('from foo')
# bar()
#
# def bar():
# print('from bar')
#
# #调用阶段
# foo()
#定义阶段:只检测语法,不执行代码
# def func():
# if 1>2
# print('hahahahahahah')
def func(): #语法没问题,逻辑有问题,引用一个不存在的变量名
asdfasdfasdfasdfasdf
函数对象:
#函数是第一类对象: 指的是函数可以被当做数据传递
def func():
print('from func')
#可被引用
# f=func
#可以当做函数的参数
# def func():
# print('from func')
# def foo(x):
# print(x)
# x()
#
# foo(func)
#可以当做函数的返回值
# def foo():
# print('from foo')
# def bar():
# return foo
# f=bar()
# print(f)
# print(foo)
# x=0
# def f1():
# x=1
# def f2():
# # x=2
# print(x)
# return f2
# f=f1()
# # print(f)
# f()
#可以当做容器类型的元素
# def select():
# print('select function')
#
# func_dic={
# 'select':select,
# }
#
# # print(func_dic['select'])
# func_dic['select']()
#
# def select():
# print('select func')
#
# def delete():
# print('delete func')
#
# def change():
# print('change func')
#
# def add():
# print('add func')
#
#
# while 1:
# cmd=input('>>: ').strip()
# if not cmd:continue
# if cmd == 'select':
# select()
# elif cmd == 'delete':
# delete()
# elif cmd == 'change':
# change()
# elif cmd == 'add':
# add()
# else:
# print('无效的命令')
def select(cmd_l):
filename=cmd_l[-1]
pattern=cmd_l[1]
with open(filename,'r',encoding='utf-8') as f:
for line in f:
if pattern in line:
print(line)
def delete():
print('delete func')
def change():
print('change func')
def add():
print('add func')
def check():
print('check func')
func_dic={
'select':select,
'delete':delete,
'change':change,
'add':add,
'check':check,
}
while 1:
inp=input('>>: ').strip()
if not inp:continue
cmd_l=inp.split()
# print(cmd_l)
cmd=cmd_l[0]
if cmd in func_dic:
func_dic[cmd](cmd_l)
else:
print('无效的命令')
函数嵌套:
#函数的嵌套调用
#
# def max2(x,y):
# if x > y:
# return x
# else:
# return y
#
# def max4(a,b,c,d):
# res1=max2(a,b) #23
# res2=max2(res1,c) #23
# res3=max2(res2,d) #31
# return res3
#
#
# print(max4(11,23,-7,31))
#函数的嵌套定义
def f1():
def f2():
def f3():
print('from f3')
print('from f2')
f3()
print('from f1')
f2()
# print(f1)
f1()
'''
from f1
from f2
from f3
'''
名称空间与作用域:
#名字空间:存放名字与值的绑定关系
#名称空间分为三种
#内置名称空间:python解释器自带的名字,python解释器启动就会生成
#全局名称空间:文件级别定义的名字都会存放与全局名称空间,执行python文件时会产生
# x=1
# def func():
# pass
#
# class Foo:
# pass
#
# import os
#
# if 1 > 2 :
# y=3
#局部名称空间:定义在函数内部的名字,局部名称空间只有在调用函数时才会生效,函数调用结束则失效
# def func(x,y): #x=1,y=2
# z=3
# func(1,2)
#三者的加载顺序:内置名称空间->全局名称空间->局部名称空间
#取值:局部名称空间->全局名称空间->内置名称空间
# # max=10
# def func(): #x=1
# # max=20
# print(max)
#
#
# func()
# max=10
#
# def func(): #x=1
# max=20
# # print(max)
# func()
#
#
# print(max)
#
# # x=0
# def f1():
# # x=1
# def f2():
# # x=2
# def f3():
# # x=3
# print(x)
# f3()
# f2()
#
# f1()
#作用域:作用范围
#全局作用域:内置名称空间与全局名称空间的名字属于全局范围,
# 在整个文件的任意位置都能被引用,全局有效
#局部作用域:局部名称空间的名字属于局部范围,
#只在函数内部可以被引用,局部有效
# x=1
# def foo():
# def f2():
# print(x)
# f2()
# def bar():
# print(x)
#
# foo()
# bar()
# def f1():
# x=1
# def f2(): #f2=value
# # x=2
# print(x)
# f2()
# f1()
x=1
def func():
x=2
def f1():pass
# print(dir(globals()['__builtins__'])) #全局作用域name
# print(locals()) #局部作用域name
func()
print(globals() is locals())
#局部作用域----->全局作用域