1 def f(a, L=[]):
2 L.append(a)
3 return L
4
5 print f(5)
6 print f(2)

输出

1 def f(a, L=None):
2 if L is None:
3 L = []
4 L.append(a)
5 return L
6
7 print f1(5)
8 print f1(2)

输出

上面两个函数的区别,f()输出的结果是累加的,f1()输出的结果是独立的

If you don’t want the default to be shared between subsequent calls -> 用f1()

4.7.2. Keyword Arguments

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print("-- This parrot wouldn't", action, end=' ')
print("if you put", voltage, "volts through it.")
print("-- Lovely plumage, the", type)
print("-- It's", state, "!") parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword

# 异常情况,下面几种情况运行都不成功
parrot()                     # required argument missing ,必须有个必填参数
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument ,这个当我把第二个参数换成status='dead'时运行成功。我的理解是前面参数用了参数名,后面就必须得跟,要是前面没跟,那后面跟不跟都可以
parrot(110, voltage=220) # duplicate value for the same argument,重复传参
parrot(actor='John Cleese') # unknown keyword argument #参数typo
 

输出

解析

传值时,带变量voltage=1000,运行时会自动匹配到对应的位置

不带变量parrot('a million', 'bereft of life', 'jump') ,则按顺序传参

定义函数有 *name和 **name时, *name 必须在 **name之前。

 1 def cheeseshop(kind, *arguments, **keywords):
2 print("-- Do you have any", kind, "?")
3 print("-- I'm sorry, we're all out of", kind)
4 for arg in arguments:
5 print(arg)
6 print("-" * 40)
7 for kw in keywords:
8 print(kw, ":", keywords[kw])
9
10 cheeseshop("Limburger", "It's very runny, sir.",
11 "It's really very, VERY runny, sir.",
12 shopkeeper="Michael Palin",
13 client="John Cleese",
14 sketch="Cheese Shop Sketch")
15
16 # 输出结果
17 -- Do you have any Limburger ?
18 -- I'm sorry, we're all out of Limburger
19 It's very runny, sir.
20 It's really very, VERY runny, sir.
21 ----------------------------------------
22 shopkeeper : Michael Palin
23 client : John Cleese
24 sketch : Cheese Shop Sketch
 
 

Python+Selenium学习笔记11 - python官网的tutorial - 定义函数的更多相关文章

  1. Python+Selenium学习笔记5 - python官网的tutorial - 交互模式下的操作

    这篇笔记主要是从Python官网的Tutorial上截取下来,再加上个人理解 1. 在交互模式下,下划线'_'还可以表示上一步的计算结果 2.引号转义问题. 从下图总结的规律是,字符串里的引号如果和引 ...

  2. Python+Selenium学习笔记14 - python官网的tutorial - just() fill() format()

    repr(x).rjust(n)  左侧空格填充,右侧列对齐,str()和repr()是一种输出,也可不用,直接x.rjust() repr(x).ljust(n)  右侧空格填充,左侧列对齐 rep ...

  3. Python+Selenium学习笔记15 - 读取txt和csv文件

    读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...

  4. Python基础学习笔记(一)python发展史与优缺点,岗位与薪资

    相信有好多朋友们都是第一次了解python吧,可能大家也听过或接触过这个编程语言.那么到底什么是python呢?它在什么机缘巧合下诞生的呢?又为什么在短短十几年时间内就流行开来呢?就请大家带着疑问,让 ...

  5. python + selenium 学习笔记 -摘要

    一.浏览器操作相关 from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() # 窗口最大 ...

  6. Python+Selenium学习笔记17 - HTML测试报告

    运行少量case时 1 # coding = utf-8 2 3 from selenium import webdriver 4 import unittest 5 import time 6 fr ...

  7. Python+Selenium学习笔记1 - pip命令

    1.用pip命令安装模块 pip install 模块名 e.g. pip install qrcode 2.用pip卸载模块 pip uninstall 模块名 e.g. pip uninstall ...

  8. Python+Selenium学习笔记18 - 不开启浏览器测试

    运行脚本时间比较长时可以不打开浏览器测试,这样在测试运行时,电脑还是可以用作其他操作的. 只需要在运行脚本上加上下面代码的678行即可 1 # coding = utf-8 2 3 from sele ...

  9. Python+Selenium学习笔记13 - 窗口截图及关闭

    涉及方法 get_screenshot_as_file() 1 # coding = utf-8 2 3 from selenium import webdriver 4 from time impo ...

随机推荐

  1. 解决mysql You can't specify target table for update in FROM clause错误

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  2. C - 抽屉 POJ - 3370 (容斥原理)

    Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...

  3. Windows核心编程 第2 4章 异常处理程序和软件异常

    异常处理程序和软件异常 C P U引发的异常,就是所谓的硬件异常(hardware exception).操作系统和应用程序 也可以引发相应的异常,称为软件异常(software exception) ...

  4. Python爬虫 XPath语法和lxml模块

    XPath语法和lxml模块 什么是XPath? xpath(XML Path Language)是一门在XML和HTML文档中查找信息的语言,可用来在XML和HTML文档中对元素和属性进行遍历. X ...

  5. C#-Stmp发邮件

    public MailMessage Initial(string Address) { MailMessage m_Mail = new MailMessage(); //发件人 m_Mail.Fr ...

  6. Mybatis-Plus02 CRUD

    先将快速开始01看完,再看这个文档 配置日志 我们所有的sql现在都是不可见的,我们希望知道它是怎么执行的,所以我们就必须看日志,开发的时候打开,上线的时候关闭 在application.proper ...

  7. Java 给Word添加数字签名

    本文以Java程序代码为例,介绍如何给Word文档添加数字签名. 程序运行环境 IntedliJ IDEA JDK 1.8.0 Jar包:spire.doc.jar 4.5.1 Word文档:.doc ...

  8. Summer——从头开始写一个简易的Spring框架

    Summer--从头开始写一个简易的Spring框架                ​ 参考Spring框架实现一个简易类似的Java框架.计划陆续实现IOC.AOP.以及数据访问模块和事务控制模块. ...

  9. SpringBoot整合shiro系列-SpingBoot是如何将shiroFilter注册到servlet容器中的

    一.先从配置类入手,主要是@Bean了一个ShiroFilterFactoryBean: @Data @Configuration @Slf4j @EnableConfigurationPropert ...

  10. iUploader 2.0 七牛云上传工具

    iUploader 软件介绍: iUploader主要功能将文件上传至七牛云,返回 Markdown 格式的链接到剪贴板 功能介绍: 图片本地压缩 图片右键上传 图片截取上传 图片复制上传 图片拖拽上 ...