`
384444165
  • 浏览: 257944 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Beginning Python 笔记学API —— Chapter5 函数

阅读更多

1、函数参数 

 

>>> # 对于函数内不可修改的类型:元组、int、字符串
>>> # 通过将值防止在列表中解决不可变
>>> def inc(x): x[0]=x[0]+1
...
>>> foo = [10]
>>> inc(foo)
>>> foo
[11]

>>> def change(n):
	n= 'new'	
>>> name = 'old'
>>> change(name)
>>> name
'old'

 

2、关键字参数

 

>>> def hello(greeting,name):
	print '%s, %s' % (greeting,name)
>>> hello('hello','world')
hello, world
>>> hello(name='world',greeting='hello')
hello, world

 

3、收集参数

 

>>> def print_params(*params): print params
>>> print_params('Test')
('Test',)
>>> print_params(1,2,3)
(1, 2, 3)

>>> # 收集关键字参数会出错
>>> print_params(params='123')
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    print_params(params='123')
TypeError: print_params() got an unexpected keyword argument 'params'

>>> # 使用**收集关键字参数为字典
>>> def print_params2(*params1,**params2):
	print params1
	print params2
>>> print_params2(1,2,3,p1=3,p2=2)
(1, 2, 3)
{'p2': 2, 'p1': 3}

 

4、反转过程(与收集参数反转)

 

>>> def add(x,y):return x+y
>>> params = (1,2)
>>> add(*params)
3
>>> params = {'name':'jason', 'greeting':'hello'}
>>> hello(**params)
hello, jason

 

分享到:
评论

相关推荐

    Beginning Python:Using Python 2.6 and Python 3.1

    ### 关于《Beginning Python:使用Python 2.6和Python 3.1》的知识点解析 #### 一、概述 本书《Beginning Python:使用Python 2.6和Python 3.1》是一本旨在帮助读者从零开始学习Python编程语言的基础书籍。作者...

    《Beginning Python:Using Python 2.6 and Python 3.1》PDF

    《 Beginning Python:Using Python 2.6 and Python 3.1》是一本旨在引导初学者入门Python编程语言的书籍,特别关注Python 2.6和3.1这两个版本。这本书涵盖了从基本语法到高级概念的广泛主题,为读者提供了一个全面...

    Beginning Python Games Development(Apress,2ed,2015)

    Beginning Python Games Development, 2nd Edition will teach you how to create visuals, do event handling, create 3D games, add media elements, and integrate OpenGL into your Python game. In this ...

    Beginning Python:Using Python 2.6 and Python 3.1-628页

    "Beginning Python:Using Python 2.6 and Python 3.1-628页" 本书籍《Beginning Python:Using Python 2.6 and Python 3.1》是Python语言的入门书籍,对于初学者和中级开发者都非常适用。该书籍涵盖了Python语言的...

    Beginning Python(Apress,3ed,2017)

    Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in Python 3, Beginning Python also covers advanced topics such as extending Python and ...

    Beginning Python From Novice to Professional(3rd) epub

    Beginning Python From Novice to Professional(3rd) 英文epub 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Python基础教程 Beginning Python From Novice to Professional.zip

    Python基础教程 Beginning Python From Novice to Professional.zip,收录了 Python基础教程 Beginning Python From Novice to Professional 中英文各版本和源码,包括(第2版)和 (第3版),都有详尽的书签,不是一级...

    Beginning Python Using Python 2.6 and Python 3.1

    ### 关于《Beginning Python: Using Python 2.6 and Python 3.1》的知识点解析 #### 第一部分:初识Python **第1章:编程基础与字符串** 本章介绍了编程的基本概念,如变量、数据类型等,并着重讲解了字符串处理...

    A Python Book Beginning Python(带书签)

    《A Python Book Beginning Python》是一本适合初学者使用的Python基础教程,作者Dave Kuhlman通过这本书为读者提供了一个自学的文档。该书分为三个部分:Python基础、高级话题以及大量的练习题。此书内容涵盖了...

    Beginning Python Using Python2.6 and Python3.1

    ### 关于《Beginning Python: Using Python 2.6 and Python 3.1》的知识点解析 #### 一、概述 本书《Beginning Python: Using Python 2.6 and Python 3.1》是一本面向初学者的Python编程入门书籍,旨在帮助读者...

    beginning python from novice to professional

    - 函数:Python中的函数定义、参数传递、返回值,以及内置函数的使用。 - 模块与包:如何导入和使用Python模块,以及创建和管理自定义的包。 2. **面向对象编程**: - 类与对象:理解类的概念,如何定义和实例化...

    Beginning Python Visualization(2nd) 无水印pdf

    Beginning Python Visualization(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...

    Beginning Python Games Development second edition

    2015年第二版,Beginning Game Development with Python and Pygame 的新版,Beginning Python Games Development, Second Edition 作者: Will McGugan 出版社: Apress 副标题: With PyGame 出版年: 2015-6-21 ...

Global site tag (gtag.js) - Google Analytics