shiniei
shiniei
发布于 2025-06-12 / 1 阅读
0
0

9.函数

函数

定义函数

>>> def greet_user():
...     '''文档字符串描述你这个函数的功能'''
...     print('hello')
...
>>> greet_user()
hello

传递参数

在定义函数中我们传递的参数叫做形参,而在我们调用函数传入的参数叫做实参

>>> def greet_user(name):
...     '''nihao'''
...     print(f'hello,{name.title()}')
...
>>> greet_user('shiniei')
hello,Shiniei

位置传参

当你定义的函数有多个形参的时候,那在你调用函数中填写的实参就要注意填写的顺序了,因为位置传参就是将形参与实参按顺序一一关联起来
这时候用这种方法关联起来的实参就叫做——位置实参

>>> def goods_price(goods, price):
...     print(f'The price of {goods} is ${price}')
...
>>> goods_price('milk', 2)
The price of milk is $2

关键字传参

实参中的值赋予给与形参名一样的实参名,因为实参名与形参名是一样的,可以就能随便改参数位置
注意:位置参数 → args(变长位置参数)→ 关键字参数 → *kwargs(变长关键字参数)

>>> def goods_price(goods, price):
...     print(f'The price of {goods} is ${price}')
...
>>> goods_price(price = 2, goods = 'milk')
The price of milk is $2

传递任意个参数

变长位置参数

传递的参数最终会变成元组

>>> def get_name(*names):
...     print(names)
...
>>> get_name('llk','moe enthusiast','lolicon')
('llk', 'moe enthusiast', 'lolicon')
>>> get_name('llk')
('llk',)

位置参数 & 变长位置参数

>>> def get_name(sex,*names):
...     print(sex,names)
...
>>> get_name('female','llk','moe enthusiast','lolicon')
female ('llk', 'moe enthusiast', 'lolicon')

变长关键字参数

>>> def build_profile(first,last,**user_info):
...     user_info['first_name'] = first
...     user_info['last_name'] = last
...     return user_info
...
>>> build_profile('宇泽', '玲纱', age = 15, height = 153, birthday = '0531')
{'age': 15, 'height': 153, 'birthday': '0531', 'first_name': '宇泽', 'last_name': '玲纱'}

设置默认值

定义函数时必须将带有默认值的形参放在后面,不然会报错

>>> def goods_price(price, goods = 'pear'):
...     print(f'The price of {goods} is ${price}')
...
>>> goods_price(2)
The price of pear is $2

可选值

>>> def get_name(first_name,last_name, middle_name = ''):
...     name = f'{first_name} {middle_name} {last_name}'
...     if middle_name != '':
...         tip = f'Your middle name is {middle_name}\n'
...     else:
...         tip = ''
...     print(f'Your first name is {first_name}.\n' + tip + f'Your last name is {last_name}')
...     print(f'Your full name is {first_name} {middle_name} {last_name}')
...
>>> get_name('mocha', 'fei', 'ba')
Your first name is mocha.
Your middle name is ba
Your last name is fei
Your full name is mocha ba fei
>>> get_name('ba', 'fei')
Your first name is ba.
Your last name is fei
Your full name is ba  fei

返回值

>>> def get_name(first_name,last_name):
...     name = f'{first_name} {last_name}'
...     return name.title()
...
>>> my_name = get_name('ba', 'fei')
>>> print(my_name)
Ba Fei

制作 & 导入模块

模块一般是带 函数 或 类 的py后缀的文件,可以自己制作,初学阶段一般会放在和其他py文件的放在同一个目录下

制作模块

# name.py
def build_profile(first,last,**user_info):
    user_info['first_name'] = first
    user_info['last_name'] = last
    return user_info

导入模块

导入全部模块、导入特定函数、导入多个函数、导入全部函数

导入全部模块

# main.py
import name
nm = name.build_profile('宇泽', '玲纱', age = 15, height = 153, birthday = '0531')
print(nm)
# {'age': 15, 'height': 153, 'birthday': '0531', 'first_name': '宇泽', 'last_name': '玲纱'}

导入特定函数

# main.py
from name import build_profile # 将 build_profile 改成 * 可以一次性导入全部函数 / 类
nm = build_profile('宇泽', '玲纱', age = 15, height = 153, birthday = '0531')
print(nm)
# {'age': 15, 'height': 153, 'birthday': '0531', 'first_name': '宇泽', 'last_name': '玲纱'}

导入多个函数

from module_name import function_0, function_1, function_2
function_0()

导入全部函数

from module_name import *
function_0()

指定别名

import module_name as mn
mn.func()
```python
from module_name import function_name as fn
fn()

评论