1.概念理解:
function:
input(x)--->function--->output(x)
- 基本实例
def test_function(a,b):
c=a+b
return c
if __name__=='__main__':
result=test_function(2,3)
print(result)
>>>5
# 更多实例
#打印功能
def print_name():
print('This is a def print test!')
print_name()
>>>This is a def print test!
#加法
def add_function(x,y):
return x+y
print(add_function(5,6))
>>>11
- 延伸的实例
#传入默认值,y默认一直是1000
#def test_function(x,y=1000):
def test_function(x,y):
print('x={}'.format(x))
print('y={}'.format(y))
return x+y
#print(test_function(100,200))
print(test_function(x=100,y=200))
#改变y的默认值,重新赋值即可
#print(test_function(x=100,y=2000))
>>>
x=100
y=200
300
# 修改为乘法,传入字符串
def test_function(x,y=5):
print('x={}'.format(x))
print('y={}'.format(y))
return x*y
print(test_function(x='King'))
>>>
x=King
y=5
KingKingKingKingKing
- 函数里面包含函数,直接拿过来用即可:
def test():pass
def test_function():
print('Hello teacher cang!!!')
test()
test_function()
>>>
Hello teacher cang!!!
# 一行搞定例子
def test_func(x,y):return x+y
>>>3
- 多个赋值:
def test_function():
return (1,2,3)
print(test_function())
# test_function()返回"1,2,3",相当于a,b,c=(1,2,3)
"""
>>> a,b,c=(1,2,3)
>>> a
1
>>> b
2
>>> c
3
"""
a,b,c=test_function()
print(a)
print(b)
print(c)
>>>
(1, 2, 3)
1
2
3
- 相当于break的---return
def test_function():
print('This is the first test!!!')
return
print('This is the second test!!!')
test_function()
>>>
# 第二个print没有执行
This is the first test!!!
- 自己设置的函数文档doc:
def test_function():
"""
This is just a test for function!!!
Remember ,it is just a test!!!
"""
print('This is the first test!!!')
# 函数不加括号,就是一个对象,这里访问它的"__doc__"特殊属性
print(test_function.__doc__)
>>>
This is just a test for function!!!
Remember ,it is just a test!!!
2.函数的属性:
def test_function():
"""
This is just a test for function!!!
Remember ,it is just a test!!!
"""
print('This is the first test!!!')
print(test_function.__doc__)
# 增加属性,函数和类一样,都可以增加属性
test_function.test=100
print(test_function.test)
print(dir(test_function))
>>>
This is just a test for function!!!
Remember ,it is just a test!!!
100
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'test']
# 访问其他属性
print(test_function.__name__)
print(test_function.__module__)
>>>
test_function
__main__
- 可变的参数---*arg:
def add_function(x,*arg):
print(x)
print(arg)
for i in arg:
x+=i
return x
print(add_function(1,2,3,4,5,6,7,8,9))
>>>
1
# 其他参数是元组的形式返回
(2, 3, 4, 5, 6, 7, 8, 9)
45
再来一个实例:
def foo(*test):
print(test)
foo('King')
foo([1,2,3])
>>>
('King',)
([1, 2, 3],)
- 传入字典的参数---**karg:
def foo(**karg):
print(karg)
foo(a=1,b=2,c=3)
>>>
{'a': 1, 'b': 2, 'c': 3}
- 综合(杂糅):
def foo(x,y,*arg,**karg):
print(x)
print(y)
print(arg)
print(karg)
foo(1,2)
foo(1,2,3,4,5)
foo(1,2,3,4,name='Jim Green')
>>>
1
2
()
{}
>>>
1
2
(3, 4, 5)
{}
>>>
1
2
(3, 4)
{'name': 'Jim Green'}
- 一种优雅的处理方式:使用元组和字典作为函数参数传进来
def add(x,y):
return (x+y)
tuple1=(2,3)
#会报错,因为只传入一个值
#print(add(tuple1))
print(add(*tuple1))
>>>
5
#出入字典,同理
>>> def book(author, name):
... print "{0}is writing {1}".format (author,name) #Python 3: print("{0}}is wri
ting {1}".format (author,name))
...
>>> bars = {"name":"Starter learning Python", "author":"Kivi"}
>>> book(**bars)
Kivi is writing Starter learning Python
网友评论