# 零基础学习 Python 之函数的应用

### 返回值

>>> def fibs(n):
...    res = [0,1]
...    for i in range(n-2):
...            res.append(res[-2] + res[-1])
...    return res
...
>>> if __name__ == "__main__":
...    now = fibs(10)
...    print(now)
...


[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]


>>> def my_digit():
...    return 1,2,3
...
>>> now = my_digit()
>>> now
(1, 2, 3)


>>> x,y,z = my_digit()
>>> x
1
>>> y
2
>>> z
3


>>> def cau():
...    pass
...
>>> now = cau()
>>> print(now)
None


>>> def my_info():
...    print('my name is rocky')
...    return
...    print('i like python')
...
>>> my_info()
my name is rocky


### 函数的文档

def fibs(n):
"""
这是一个求斐波那契数列的函数
"""


>>> dir(type)
['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__text_signature__', '__weakrefoffset__', 'mro']
>>> type.__doc__
"type(object_or_name, bases, dict)\ntype(object) -> the object's type\ntype(name, bases, dict) -> a new type"


### 函数的属性

>>> def cau():
...    """this is a cau function"""
...    pass
...


>>> cau.__doc__
'this is a cau function'


>>> cau.pig = 100
>>> cau.pig
100


>>> dir(cau)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'pig']


### 写在之后

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客