search
HomeBackend DevelopmentPython Tutorial初学Python函数的笔记整理

 定义
返回单值

def my_abs(x):
  if x >= 0:
    return x
  else:
    return -x
 

返回多值

返回多值就是返回一个tuple

import math
 
def move(x, y, step, angle=0):
  nx = x + step * math.cos(angle)
  ny = y - step * math.sin(angle)
  return nx, ny

空函数
 

def nop():
  pass

指定默认参数

必选参数在前,默认参数在后。默认参数需指向不可变对象(默认参数值在函数定义时被计算)
 

def power(x, n=2):
  s = 1
  while n > 0:
    n = n - 1
    s = s * x
  return s

可变参数
 

def calc(*numbers):
  sum = 0
  for n in numbers:
    sum = sum + n * n
  return sum

调用可变参数的函数方法
 

>>> calc(1, 2)
5
>>> calc()
0
>>> nums = [1, 2, 3]
>>> calc(*nums)
14

关键字参数
 

def person(name, age, **kw):
  print 'name:', name, 'age:', age, 'other:', kw

调用关键字参数的方法
 

>>> person('Michael', 30)
name: Michael age: 30 other: {}
>>> person('Bob', 35, city='Beijing')
name: Bob age: 35 other: {'city': 'Beijing'}
>>> person('Adam', 45, gender='M', job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}
>>> kw = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, **kw)
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

注:

    参数定义的顺序必须是:必选参数、默认参数、可变参数和关键字参数。
    对于任意函数,都可以通过类似func(*args, **kw)的形式调用它,无论它的参数是如何定义的。

递归

如果一个函数在内部调用自身本身,这个函数就是递归函数。
尾递归

在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。
高阶函数

  •     变量可以指向函数(函数可以赋值给一个变量)
  •     函数名也是变量(函数名可以赋值其他值)
  •     函数可以做为函数的参数(高阶函数)

map(func, list)

map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。
 

>>> def f(x):
...   return x * x
...
>>> map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
reduce(func_with_two_params, list)

reduce把一个函数作用在一个序列[x1, x2, x3…]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算。
 

reduce(f, [x1, x2, x3, x4])
#相当于:
f(f(f(x1, x2), x3), x4)
 
>>> def add(x, y):
...   return x + y
...
>>> reduce(add, [1, 3, 5, 7, 9])
25

filter(func_return_bool, list)

把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
 

def is_odd(n):
  return n % 2 == 1
 
filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
# 结果: [1, 5, 9, 15]

sorted

对于两个元素x和y,如果认为x y,则返回1,
 

>>> sorted([36, 5, 12, 9, 21])
[5, 9, 12, 21, 36]

高阶函数用法
 

def reversed_cmp(x, y):
  if x > y:
    return -1
  if x < y:
    return 1
  return 0
 
>>> sorted([36, 5, 12, 9, 21], reversed_cmp)
[36, 21, 12, 9, 5]

函数做为返回值
 

def lazy_sum(*args):
  def sum():
    ax = 0
    for n in args:
      ax = ax + n
    return ax
  return sum
 
>>> f = lazy_sum(1, 3, 5, 7, 9)
>>> f
<function sum at 0x10452f668>
>>> f()
25

注:每次调用lazy_sum()都会返回一个新的函数,即使传入相同的参数。
闭包
 

def count():
  fs = []
  for i in range(1, 4):
    def f():
       return i*i
    fs.append(f)
  return fs
 
f1, f2, f3 = count()
>>> f1()
9
>>> f2()
9
>>> f3()
9

原因是调用count的时候循环已经执行,但是f()还没有执行,直到调用其时才执行。所以返回函数不要引用任何循环变量,或者后续会发生变化的变量。
匿名函数(lambda表达式)
 

等价于:
 

def f(x):
  return x * x

关键字lambda表示匿名函数,冒号前面的x表示函数参数。
匿名函数做为返回值
 

def build(x, y):
  return lambda: x * x + y * y

装饰器(@func)

在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator),本质上,decorator就是一个返回函数的高阶函数。
 

def log(func):
  def wrapper(*args, **kw):
    print 'call %s():' % func.__name__
    return func(*args, **kw)
  return wrapper
 
@log
def now():
  print '2013-12-25'
 
>>> now()
call now():
2013-12-25
 
#相当于执行:
 
now = log(now)
回到顶部
带参数的装饰器
 
def log(text):
  def decorator(func):
    def wrapper(*args, **kw):
      print '%s %s():' % (text, func.__name__)
      return func(*args, **kw)
    return wrapper
  return decorator
 
@log('execute')
def now():
  print '2013-12-25'
 
#执行结果
>>> now()
execute now():
2013-12-25
 
#相当于执行:
 
>>> now = log('execute')(now)

剖析:首先执行log('execute'),返回的是decorator函数,再调用返回的函数,参数是now函数,返回值最终是wrapper函数。

__name__
由于函数的__name__已经改变,依赖于此的代码就会出错。因此使用functools.wraps。
 
import functools
 
def log(func):
  @functools.wraps(func)
  def wrapper(*args, **kw):
    print 'call %s():' % func.__name__
    return func(*args, **kw)
  return wrapper
 
#对于带参函数
 
import functools
 
def log(text):
  def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kw):
      print '%s %s():' % (text, func.__name__)
      return func(*args, **kw)
    return wrapper
  return decorator
偏函数(固定函数默认值)
 
>>> import functools
>>> int2 = functools.partial(int, base=2)
>>> int2('1000000')
64
>>> int2('1010101')
85
 
#相当于:
 
def int2(x, base=2):
  return int(x, base)
 
max2 = functools.partial(max, 10)

相当于为max函数指定了第一个参数
 

max2(5, 6, 7)
 
#相当于:
 
max(10, 5, 6, 7)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software