#文档字符串
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. 2017 ACM/ICPC 广西邀请赛 题解

    题目链接  Problems HDOJ上的题目顺序可能和现场比赛的题目顺序不一样, 我这里的是按照HDOJ的题目顺序来写的. Problem 1001 签到 #include <bits/std ...

  2. T3186 队列练习2 codevs

    http://codevs.cn/problem/3186/ 题目描述 Description (此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)给定一个队列(初始为空),只有两种 ...

  3. 洛谷——P1746 离开中山路

    P1746 离开中山路 题目背景 <爱与愁的故事第三弹·shopping>最终章. 题目描述 爱与愁大神买完东西后,打算坐车离开中山路.现在爱与愁大神在x1,y1处,车站在x2,y2处.现 ...

  4. 提高google网站访问速度

    修改:C:\Windows\System32\drivers\etc\hosts文件 # google websites.203.208.46.180 ssl.gstatic.com203.208.4 ...

  5. IntelliJ IDEA设置properties文件显示中文

    配置这里: 注意:上面是Default Settings,还需要在Settings中设置成上面一样的.

  6. 开源天气预报api整理

    高德天气:https://lbs.amap.com/api/webservice/guide/api/weatherinfo/? github上对开源api的整理:https://github.com ...

  7. 【spring boot Mybatis】报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.interview.dao.UserMapper.add

    报错如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.newhope.i ...

  8. [Adobe Analytics] Segments types

    There are three types of Segmentation Hit-based Visit-based Visitor-based There are four segment con ...

  9. shell 检查文件夹所属用户组

    shell 检查文件夹所属用户组 #!/bin/bash # 检查文件夹 权限是否777 检查文件夹所属组是否www # authro ranmufei # 2017 08 21 云板容器版 /dat ...

  10. 当 外部 input 值的改变,获取 当前 input type="hidden" 的值

    1.如何用jquery获取<input id="test" name="test" type="text"/>中输入的值? 方法 ...