We have seen built-in functions that take a variable number of arguments. For example range can take one, two or three arguments.

It is possible to write user-defined functions with optional arguments, too. For example here is a function that prints the most common words in a histogram:

If you provide one argument number gets the default value. If you provide two arguments, num gets the value of the argument instead. In other words, the optional argument overrides the default value.

If a function has both required and optional parameters, all the required parameters have to come first, followed by the optional ones.

from Thinking in Python

Optional arguments的更多相关文章

  1. 命名实参和可选实参 Named and Optional Arguments

    1. 利用“命名实参”,您将能够为特定形参指定实参,方法是将实参与该形参的名称关联,而不是与形参在形参列表中的位置关联. static void Main(string[] args) { Conso ...

  2. jquery function Optional Arguments

    1.javascript 选项散列对象 function Test(p1,p2,p3,p4,p5){ //do something } call: 参数可选 Test({ p1:value1, p2: ...

  3. Python高手之路【二】python基本数据类型

    一:数字 int int(整型): 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值 ...

  4. python基础

    内容概要: 一.python2 or python3 目前大多使用python2.7,随着时间的推移,python3将会成为python爱好者的主流. python2和3区别: 1.PRINT IS ...

  5. Python标准模块--argparse

    1 模块简介 你一定很奇怪Python是如何命令行中的变量的吧?argparse就是用来解决这个问题的,argparse是optparse的替代. 2 模块使用 2.1 开始 我发现解释一个编程的概念 ...

  6. DotNet 资源大全中文版(Awesome最新版)

    Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...

  7. 读书笔记--SQL必知必会--Tips

    01 - 如何获取SQL命令帮助信息 官方手册 help 或 help command MariaDB [(none)]> help General information about Mari ...

  8. Python基础(二)

    本章内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和x ...

  9. python-基本数据类型

    /int整数/ 如: 18.73.84 每一个整数都具备如下功能: class int(object): """ int(x=0) -> int or long i ...

随机推荐

  1. vue中export default 在console中是this.$vm

    vue中export default 在console中是this.$vm 用vue-cli搭出框架,用webstorm进行开发,参考vue2的官网进行教程学习, 在vue-cli中是用es6的exp ...

  2. [Python Test] Use pytest fixtures to reduce duplicated code across unit tests

    In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resou ...

  3. [CSS3] The different of Background-size between 'cover' and 'contain'

    'cover': The smaller axies of image (x axies) should match smaller axies (x axies) of container. So ...

  4. c++变量的作用域、生存期和可见性

    局部变量 范围:在一个函数内部定义的变量,作用范围仅仅限于本函数体内. 生存期:程序运行到本函数才会给局部变量分配内存单元.函数运行完成局部变量所占的存储单元就被释放 全局变量 在函数体外部定义的变量 ...

  5. [codeforces 1037D] Valid BFS? 解题报告(验证bfs序,思维题)

    题目链接:http://codeforces.com/problemset/problem/1037/D 题目大意: 给出一棵树,询问一个序列是否可能为这棵树从节点1开始遍历的bfs序 题解: 对于每 ...

  6. HD-ACM算法专攻系列(6)——Big Number

    题目描述: 源码: #include"iostream" #include"cmath" using namespace std; #define PI 3.1 ...

  7. quartz.net定时任务框架详解

    C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...

  8. 滚动监听 after选择器

    一.如何实现滚动到一定位置将内容固定在页面顶部 window.onscroll=function(){ //滚动的距离,距离顶部的距离 var topScroll =document.body.scr ...

  9. Java文件(io)编程——文件字符流的使用

    案例1: 读取一个文件并写入到另一个文件中,char[] 来中转. 首先要在E盘下创建一个文本文档,命名为test.txt,输入一些字符串. public class Demo_5 { public ...

  10. 洛谷P2770 航空路线问题 最小费用流

    Code: #include<cstdio> #include<iostream> #include<algorithm> #include<vector&g ...