function.py
#文档字符串
def square(x):
'calculates the square of the number x'
return x*x
square.__doc__
help(square) #列表做实参
#当2个变量同时引用一个列表时,他们的确是同时引一个列表用
#当在序列中做切片时,返回的切片总是一个副本
def change(x):
x[0]='***'
name1=['aaa','bbb'] #['***', 'bbb']
name2=['aaa','bbb'] #['aaa', 'bbb']
change(name1)
change(name2[:])
print(name1,name2) #函数应用
def init(data):
data['first']={}
data['middle']={}
data['end']={}
def lookup(data,label,name):
return data[label].get(name)
def store(data,full_name):
names=full_name.split();
if len(names)==2: names.insert(1,'')
labels='first','middle','end'
for label,name in zip(labels,names):
people=lookup(data,label,name)
#print(people,full_name,label,name)
if people:
people.append(full_name)
else:
data[label][name]=[full_name] MyName={}
init(MyName) store(MyName,'Ma LIe Het')
print(lookup(MyName,'middle','LIe')) store(MyName,'Robin Hood')
store(MyName,'Robin Loksley')
print(lookup(MyName,'first','Robin'))
print(lookup(MyName,'middle','')) #关键字参数,默认值
def hello(greet='hello',name='world'):
print('%s, %s' % (greet,name))
hello()
hello('Greeting')
hello('Greeting','CC')
hello(name='CC')
def hello_2(name,greet='hello'):
print('%s,%s' % (greet,name))
try:
hello_2() #error missing 1 required positional argument: 'name'
except Exception as e:
print(e) #收集参数
#*收集成元组
def print_1(*arg):
print(arg)
def print_2(name,*arg):
print(name)
print(arg)
def print_3(*arg,name):
print(arg)
print(name)
print_1('abc') #('abc',)元组
print_1(1,2,3) #(1,2,3)
print_2('no') #no ()
try:
print_3(1,2,3) #error
except Exception as e:
print(e)
print_3(1,2,name=3) #OK
#**收集[关键字参数]成字典
def print_para(**arg): #必须是关键字参数,不能是字典
print(arg) #arg变成了字典
print_para(x=1,y=2,z=3) #{'z': 3, 'y': 2, 'x': 1}
a={'z': 3, 'y': 2, 'x': 1}
try:
print_para(a) #error 参数不能是字典,而应是关键字参数
except Exception as e:
print(e)
def print_dict(**arg):
print('%(name)s, is %(job)s' % arg)
print_dict(job='a',name=2) #2 is a
def print_para_2(x,y=1,*p1,**p2):
print(x,y)
print(p1)
print(p2)
print_para_2(1,2,3,4,a=1,b=2) #1 2 (3,4) {'a': 1, 'b': 2}
print_para_2(1) #1 1 () {}
#拓展
def test(size):
a,b=size
try:
test(1,2) #error
test(1) #error
except Exception as e:
print(e)
a=(1,2)
test(a) #OK
try:
test(*a) #error
except Exception as e:
print(e) #参数反转
#*号只在定义函数或调用时才有用
def add(a,b): return a+b
t=(1,2)
add(*t) #t元素个数要与add参数个数一致
def hello_2(name,greet='hello'):
print('%s,%s' % (greet,name))
t={'greet':'Well','name':'CC'}
hello_2(**t) #t元素个数要与hello_2个数一致,对应 print('---嵌套函数---')
def f1(x):
print("x=",x)
def f2(y):
print('x=',x,'y=',y)
return x*y
return f2 f1(2)(3) #x=2
m=f1(2)
m(3)
function.py的更多相关文章
- Mac OSX bash function 备份
# mount the android file image function mountAndroid { hdiutil attach ~/android.dmg.sparsefile.spars ...
- python mock基本使用
什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...
- 【译】Python中如何创建mock?
原文地址:http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ 今天我们来谈论下mock的使用.当然,请不要误会,这里的m ...
- Python:模块引用
#!/usr/bin/python3 #Filename function.py #导入模块 import sys #导入function.py#function.py 文件import functi ...
- python自学笔记
python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...
- Selenium2+Python自动化测试实战
本人在网上查找了很多做自动化的教程和实例,偶然的一个机会接触到了selenium,觉得非常好用.后来就在网上查阅各种selenium的教程,但是网上的东西真的是太多了,以至于很多东西参考完后无法系统的 ...
- 2nd_SE-结对编程1-基于flask框架的四则运算生成器
0x00 Coding https://coding.net/u/nikochan/p/2nd_SE/git 0x01 写在前面 因为在上一个作业中,是基于python完成的Command程序.那么再 ...
- 结对作业1----基于flask框架的四则运算生成器
011.012结对作业 coding地址:https://coding.net/u/nikochan/p/2nd_SE/git 一.作业描述 由于上次作业我没有按时完成,而且庞伊凡同学编程能力超棒,所 ...
- 展示博客(Beta阶段)
展示博客 0x00 团队成员 成员 博客地址 简介 黄建英 http://www.cnblogs.com/smilehjy/ beta阶段的新成员,负责前端界面调整 谢晓萍 http://www.cn ...
随机推荐
- @font-face制作小图标的实践
1.为啥要用font-face制作小图标 1)适用性:一个图标字体要比一系列的图像要小,一旦字体图标加载完,图标则会立刻显示出来,不需要去下载一个图像. 2)可扩展性:可以使用font-size对图标 ...
- InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pa
2016-09-14T09:17:37.713955Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleane ...
- oracle存储过程中使用字符串拼接
1.使用拼接符号“||” v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAccount || '' ...
- linux 每天备份mysql数据
原文:http://www.open-open.com/code/view/1433587311890 前言: 如果你的数据库文件较大,可能备份的时候需要占用很多服务器资源,所以站长要尽量选择访客最少 ...
- js long类型的日期转成Date,字符串StringBuilder拼接
longToDate.js //扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+" ...
- Mac--安装kubernetes并运行echoserver
安装minikube curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.15.0/minikube-darwi ...
- linux批量解压和批量压缩
ls *.tar.gz | xargs -n1 tar xzvf //批量解压 ls | awk '{ print "tar zcvf "$0".tar.gz " ...
- 函数柯里化 curry
一.函数柯里化的特性: (1)参数复用 $.ajax // 示例一 function ajax(type,url,data) { var xhr = new XMLHttpRequest(); xhr ...
- vim调用python格式化json数据
vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...
- css设置图片居中、居左、居右
CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...