python 函数

作者: 森先生_wood | 来源:发表于2015-10-28 00:33 被阅读67次

参数

  • 定义函数
    位置参数
    关键字参数:设置默认值
    收集参数:(*p,**f)将多余的参数转化成元组和字典。
  • 调用函数
    参数收集的逆过程:(*list,**dic)将元组和字典转化成参数

作用域

变量名和值的对应关系相当于字典里的键和值,vars函数可以返回这个字典。
>>> x=1
>>> vars()['x']
1
在函数内部访问全局变量的方法:
1,没有和其重名的局部变量:只需要读取的话直接访问,需要重绑定的话声明其为全局变量:global 变量名
>>> x=1
>>> def a():
... print x
...
>>> a()
1
>>> def a():
... global x
... x=x+1
...
>>> a()
>>> x
2
>>> def a():
... print x
... x=x+1
...
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in a
UnboundLocalError: local variable 'x' referenced before assignment
2,有和其重名的局部变量:globals()['变量名']

相关文章

  • Python - 2017/01/28-函数

    调用python内置函数 函数名(参数) 即可调用python内置函数 help(函数名) 返回python对于函...

  • Python函数式介绍一 - 高阶函数

    Python函数式介绍一 - 高阶函数Python函数式介绍二 - 链式调用 最近为了给朋友推广Python函数式...

  • Python高阶函数学习笔记

    python中的高阶函数是指能够接收函数作为参数的函数 python中map()函数map()是 Python 内...

  • Python学习笔记1

    Python注释 Python变量 Python运算符 Python输入输出 输入函数 输出函数(3.x) ...

  • Python:内置函数

    python的内置函数,匿名函数 内置函数 内置函数就是python给你提供的,拿来直接用的函数,比如print,...

  • 二级Python----Python的内置函数及标准库(DAY

    Python的内置函数 嵌入到主调函数中的函数称为内置函数,又称内嵌函数。 python的内置函数(68个) Py...

  • python3 range() 函数和 xrange() 函数

    python3 range 函数 python3 取消了 xrange() 函数,并且和 range() 函数合并...

  • 7、函数

    1、Python之什么是函数 2、Python之调用函数 Python内置了很多有用的函数,我们可以直接调用。 要...

  • Python入门

    Python3教程 安装Python 第一个Python程序 Python基础 函数 高级特性 函数式编程 模块 ...

  • Python函数详解

    函数是Python里组织代码的最小单元,Python函数包含以下几个部分: 定义函数 调用函数 参数 函数的返回值...

网友评论

    本文标题:python 函数

    本文链接:https://www.haomeiwen.com/subject/cxwxhttx.html