Python 函数

def 语句

函数用def语句创建,语法如下:
def function_name(arguments):
    “function_documentation_string”
      function_body_suite
标题行由def关键字,函数的名字,以及参数的集合(如果有的话组成),def子句的剩余部分包括了一个虽然可选但是强烈推荐的文档字符串,和必须的函数体
 
#!/usr/bin/env python

def foo():
    print "This foo"
    bar()

def bar():
    print "This bar"

if __name__ == '__main__':
    foo()

/usr/bin/python2.6 /root/PycharmProjects/untitled10/foo.py
This foo
This bar

Process finished with exit code 0

函数属性

函数属性是python另外一个使用了句点属性标识并拥有名字空间领域
>>> def foo():
...     'foo() --properly create doc string'
... 
>>> foo.__doc__
'foo() --properly create doc string'
>>> foo.version
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'version'
>>> foo.version = 1.0
>>> foo.version
1.0

内部函数

在函数体内创建另外一个函数是完全合法的,这种函数叫做内部/内嵌函数
#!/usr/bin/env python


def foo():
    print "This foo"
    def bar():
        print "This is bar"
    bar()


if __name__ == '__main__':
    foo()


/usr/bin/python2.6 /root/PycharmProjects/untitled10/foo.py
This foo
This is bar

Process finished with exit code 0

函数操作符

使用一堆圆括号()调用函数,如果没有圆括号,只是对函数的引用,任何输入的带参数都必须放置在括号中
引用
调用
>>> def foo():
...     print 'in foo'
... 
>>> a = foo
>>> a()
in foo
>>> foo()
in foo
>>> foo
<function foo at 0x7f3a3fbfa6e0>
>>> a
<function foo at 0x7f3a3fbfa6e0>

关键字参数

关键字参数的概念仅仅针对函数的调用,这种理念是让调用者通过函数调用中的参数名字来区分参数
>>> def get_info(name,age):
...     print "%s is %s year old" % (name,age)
... 
>>> get_info('bob',20)
bob is 20 year old
>>> get_info(20,'bob')
20 is bob year old
>>> get_info(age = 20,name = 'bob')
bob is 20 year old
>>> get_info(age = 20,name = 'bob')
bob is 20 year old
>>> get_info(age = 20,'bob')
  File "<stdin>", line 1
SyntaxError: non-keyword arg after keyword arg
>>> get_info(20,name = 'bob')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get_info() got multiple values for keyword argument 'name'
>>> get_info('bob',age = 20)
bob is 20 year old
>>> get_info()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get_info() takes exactly 2 arguments (0 given)
>>> get_info('bob',20,30)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get_info() takes exactly 2 arguments (3 given)
>>> def pstar(num = 20):
...     print '*' * num
... 
>>> pstar()
********************
>>> pstar(30)
******************************
>>> pstar(20,30)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pstar() takes at most 1 argument (2 given)

参数组

python允许程序员执行没有显示定义参数的函数
相应的方法是通过一个把元组(非关键字参数)或字典(关键字参数)作为参数组传递给函数
>>> def foo(*args):
...     print args
... 
>>> foo()
()
>>> foo(10)
(10,)
>>> foo(10,20,'hello')
(10, 20, 'hello')
>>> def add(x,y):
...     return x + y
... 
>>> add(10,10)
20
>>> add([10,10])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add() takes exactly 2 arguments (1 given)
>>> add(*[10,10])
20
>>> def func1(args,*non_kwargs,**kwargs):
...     print args
...     print non_kwargs
...     print kwargs
... 
>>> func1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func1() takes at least 1 argument (0 given)
>>> func1(10)
10
()
{}
>>> func1(10,20,30,name='bob')
10
(20, 30)
{'name': 'bob'}

 

 
 
 
 
 
 
 
 
posted @ 2017-03-29 14:43  Time.catcher  阅读(225)  评论(0编辑  收藏  举报