#文档字符串
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的更多相关文章

  1. Mac OSX bash function 备份

    # mount the android file image function mountAndroid { hdiutil attach ~/android.dmg.sparsefile.spars ...

  2. python mock基本使用

    什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...

  3. 【译】Python中如何创建mock?

    原文地址:http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ 今天我们来谈论下mock的使用.当然,请不要误会,这里的m ...

  4. Python:模块引用

    #!/usr/bin/python3 #Filename function.py #导入模块 import sys #导入function.py#function.py 文件import functi ...

  5. python自学笔记

    python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...

  6. Selenium2+Python自动化测试实战

    本人在网上查找了很多做自动化的教程和实例,偶然的一个机会接触到了selenium,觉得非常好用.后来就在网上查阅各种selenium的教程,但是网上的东西真的是太多了,以至于很多东西参考完后无法系统的 ...

  7. 2nd_SE-结对编程1-基于flask框架的四则运算生成器

    0x00 Coding https://coding.net/u/nikochan/p/2nd_SE/git 0x01 写在前面 因为在上一个作业中,是基于python完成的Command程序.那么再 ...

  8. 结对作业1----基于flask框架的四则运算生成器

    011.012结对作业 coding地址:https://coding.net/u/nikochan/p/2nd_SE/git 一.作业描述 由于上次作业我没有按时完成,而且庞伊凡同学编程能力超棒,所 ...

  9. 展示博客(Beta阶段)

    展示博客 0x00 团队成员 成员 博客地址 简介 黄建英 http://www.cnblogs.com/smilehjy/ beta阶段的新成员,负责前端界面调整 谢晓萍 http://www.cn ...

随机推荐

  1. HUNAN -11566 Graduation Examination(找规律)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11566&courseid=0 输入n,求出第n个fi ...

  2. Linux最常用的命名

    一.环境配置 vim /etc/sysconfig/network-scripts/ifcfg-eth0 vim /etc/sysconfig/network vim /etc/hostname vi ...

  3. 局域网Cesium离线影像及瓦片影像地图加载

    1.Cesium简介 优点: cesium展示地图数据效果比较好,解析2D地图各种不同服务类型的数据源,比如百度地图.天地图.arcgis地图.BingMap.openStreetMap.MapBox ...

  4. Oracle释放高水位线

    /*****************************************************************原因:由于原导出数据库没有整理表空间其中主要包括两方面,一是用户产生 ...

  5. IOS开发 二维码功能的实现

    原帖地址:http://yul100887.blog.163.com/blog/static/20033613520121020611299/ 如今二维码随处可见,无论是实物商品还是各种礼券都少不了二 ...

  6. BUPT复试专题—科学计算器(2009)

    题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...

  7. remove_if的问题

    #include<iostream> #include<list> #include<algorithm> #include"PRINT_ELEMENTS ...

  8. PHPMailer发送邮件乱码

    PHPMailer发送邮件乱码, $mail->CharSet="GB2312";$mail->Encoding = "base64"; 设成这样不 ...

  9. Qt中 QString 和int,double等的转换

    Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...

  10. [转载]C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { , i = ; ; ; isspace( ...