函数
目录
Python中的函数使用 def
来进行声明。Python中的函数可以接受两种类型的参数:普通参数和命名参数(named arguments)。
In [1]: def check_num(num, ignore=False):
...: if ignore:
...: return
...:
...: if num < 0:
...: print("num < 0")
...: elif num == 0:
...: print("num == 0")
...: else:
...: print("num > 0")
...:
In [2]: check_num()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-e2de90bec545> in <module>
----> 1 check_num()
TypeError: check_num() missing 1 required positional argument: 'num'
In [3]: check_num(1)
num > 0
In [4]: check_num(2)
num > 0
In [5]: check_num(2, ignore=True)
In [6]: check_num(2, True)
如果 return
后面不接返回值,那么默认将会返回 None
。
更多文章
本站热门
- socks5 协议详解
- zerotier简明教程
- 搞定面试中的系统设计题
- frp 源码阅读与分析(一):流程和概念
- 用peewee代替SQLAlchemy
- Golang(Go语言)中实现典型的fork调用
- DNSCrypt简明教程
- 一个Gunicorn worker数量引发的血案
- Golang validator使用教程
- Docker组件介绍(二):shim, docker-init和docker-proxy
- Docker组件介绍(一):runc和containerd
- 使用Go语言实现一个异步任务框架
- 协程(coroutine)简介 - 什么是协程?
- SQLAlchemy简明教程
- Golang的template(模板引擎)简明教程