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 ...
随机推荐
- js等待提示通用类
function WaitingTip (options){ if(!options){ options = { contain ...
- SQL自动生成A到Z二十六个英文字母
if object_id('#tempdriveinfo') is not null drop table #tempdriveinfo create table #tempdriveinfo ( [ ...
- C++ 使用成员初始化列表的一个小坑
注意在成员列表中初始化的顺序并不是列表顺序 而是: 在类中声明的顺序! EventLoop::EventLoop() :looping(false), quit(false),_tid(curThre ...
- java.nio.ByteBuffer 以及flip,clear及rewind区别
Buffer 类 定义了一个可以线性存放primitive type数据的容器接口.Buffer主要包含了与类型(byte, char…)无关的功能. 值得注意的是Buffer及其子类都不是线程安全的 ...
- Linux出现cannot create temp file for here-document: No space left on device的问题解决
在终端输入:cd /ho 按tab键时,显示错误: bash: cannot create temp file for here-document: No space left on device 这 ...
- Mysql 性能优化20个原则(2)
5. 在Join表的时候使用相当类型的例,并将其索引 如果你的应用程序有很多 JOIN 查询,你应该确认两个表中Join的字段是被建过索引的.这样,MySQL内部会启动为你优化Join的SQL语句的机 ...
- Android:BLE智能硬件开发详解
目录 前言 BLE是个什么鬼 BLE中的角色分工 主要的关键词和概念 GATT(Generic Attribute Profile ) Characteristic Service Android如何 ...
- tcp三次握手和syn 洪水攻击
1. 连接后,所有的 ack 为1才有效(连接后,ack 也一般都是1) 2. 建立连接3次握手, 如何确认对方收到了你发的包, seq 是自己发出去的,自己知道seq的值.所以怎么确认对方收到了自己 ...
- c程序设计语言第一章4
(昨天网络出现了问题,导致这篇没来得及上传,再次补上,今晚照常上传笔记) 练习1.19编写函数r e v e r s e ( s )将字符串s中的字符顺序颠倒过来.使用该函数编写一个程序,每次颠倒一个 ...
- Elasticsearch 之 慘痛部署(分片移位)
部署说明 硬件 server两台: 机器A:64G内存 机器B:32G内存 分片 共12个节点 2个查询节点.10个存储节点 8个主分片 1个复制分片(每一个分片都有一个副本分布在不同的节点上面) 每 ...