python function parameter

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> def function():定义函数
    ptintf("run")

    
>>> function()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    function()
  File "<pyshell#2>", line 2, in function
    ptintf("run")
NameError: global name 'ptintf' is not defined
>>> def fun():
    print ("hello")

    
>>> fun()
hello
>>> def fun2(name):
    print(name)

    
>>> fun2(new)

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    fun2(new)
NameError: name 'new' is not defined
>>> fun2("new")
new
>>> 
>>> 
>>> def fun3(num1,num2):
    return num1+num2函数的返回值

>>> print (fun3(1,2))
3
>>> def fun4(name)
SyntaxError: invalid syntax
>>> def fun4(name):
    'name is a param'
    print(name)

    
>>> fun4("hello world")
hello world
>>> fun4._doc_函数的注释文档

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    fun4._doc_
AttributeError: 'function' object has no attribute '_doc_'
>>> help(fun4)
Help on function fun4 in module __main__:

fun4(name)
    name is a param

>>> def fun5(*param)
SyntaxError: invalid syntax
>>> def fun5(*param):
    print ("%d",len(param))
    print (param)

    
>>> fun5(1,2,3,4)
('%d', 4)
(1, 2, 3, 4)
>>> def fun5(*param,par):
    print ("%d",len(param))
    print (param)
    
SyntaxError: invalid syntax
>>> 
>>> def fun6(*param,par):函数的收集参数
    print ("%d",len(param))
    print (param)
    
SyntaxError: invalid syntax
>>> def fun7(*param,name):
    
SyntaxError: invalid syntax
>>> def fun8(*pa , name1):
    
SyntaxError: invalid syntax
>>> 

Python 2.7.10 (default, Oct 14 2015, 16:09:02) [GCC 5.2.1 20151010] on linux2Type "copyright", "credits" or "license()" for more information.>>> def function():ptintf("run")
>>> function()
Traceback (most recent call last):  File "<pyshell#3>", line 1, in <module>    function()  File "<pyshell#2>", line 2, in function    ptintf("run")NameError: global name 'ptintf' is not defined>>> def fun():print ("hello")
>>> fun()hello>>> def fun2(name):print(name)
>>> fun2(new)
Traceback (most recent call last):  File "<pyshell#11>", line 1, in <module>    fun2(new)NameError: name 'new' is not defined>>> fun2("new")new>>> >>> >>> def fun3(num1,num2):return num1+num2
>>> print (fun3(1,2))3>>> def fun4(name)SyntaxError: invalid syntax>>> def fun4(name):'name is a param'print(name)
>>> fun4("hello world")hello world>>> fun4._doc_
Traceback (most recent call last):  File "<pyshell#25>", line 1, in <module>    fun4._doc_AttributeError: 'function' object has no attribute '_doc_'>>> help(fun4)Help on function fun4 in module __main__:
fun4(name)    name is a param
>>> def fun5(*param)SyntaxError: invalid syntax>>> def fun5(*param):print ("%d",len(param))print (param)
>>> fun5(1,2,3,4)('%d', 4)(1, 2, 3, 4)>>> def fun5(*param,par):print ("%d",len(param))print (param)SyntaxError: invalid syntax>>> >>> def fun6(*param,par):print ("%d",len(param))print (param)SyntaxError: invalid syntax>>> def fun7(*param,name):SyntaxError: invalid syntax>>> def fun8(*pa , name1):SyntaxError: invalid syntax>>> 

原文地址:https://www.cnblogs.com/13224ACMer/p/6045471.html