常见面试题之*args

本文深入解析了Python中函数参数的使用方法,特别是默认参数的覆盖机制。通过具体示例,展示了如何利用列表传递多个参数给函数,并解释了参数传递过程中默认值被实际参数覆盖的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


这个地方理解即可,只是面试的时候会被问到,单独做了一下知识点的整理,不推荐使用。

def self_max(a,b,c,d,e,f,g,h,k,x=1,y=3,z=4):    #默认参数
    
    print(a,b,c,d,e,f,g,h,k,x,y,z)

args=[1,2,3,4,5,6,7,8,9,20,30,56]    
把这个列表内的元素一个一个的取出来,然后一个一个的传给这个函数的形参,
取出来的值会覆盖默认参数,所以x=1,y=3,z=4的值在执行函数后改变为x=20,y=30,z=56

self_max(*args)

打印结果是:
1 2 3 4 5 6 7 8 9 20 30 56


转载于:https://www.cnblogs.com/ludundun/p/11519462.html

### 华为 Python 常见面试题汇总 以下是针对华为 Python 面试题常见考点及其解析: #### 数据结构与算法 1. **快速排序实现** 快速排序是一种分治法策略的经典应用,其基本思想是通过选取一个基准值将数组划分为两部分[^1]。 ```python def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) ``` 2. **链表反转** 反转单向链表是一个常见的数据结构问题,在实际工程中有广泛应用场景。 ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_list(head): prev = None current = head while current is not None: next_node = current.next current.next = prev prev = current current = next_node return prev ``` #### Pandas 数据处理 3. **判断 DataFrame 中是否存在缺失值** 使用 `pandas` 的内置函数可以高效检测并处理缺失值[^4]。 ```python import pandas as pd df = pd.DataFrame({'A': [1, 2, None, 4], 'B': [5, None, 7, 8], 'C': [9, 10, 11, None]}) missing_values = df.isnull() print(missing_values) ``` #### 文件操作与序列化 4. **Pickle 模块的应用** Pickle 是 Python 提供的一种对象持久化工具,能够将复杂的数据结构保存到文件中,并在后续加载使用[^3]。 ```python import pickle data = {'name': 'Alice', 'age': 25, 'city': 'New York'} with open('data.pkl', 'wb') as f: pickle.dump(data, f) with open('data.pkl', 'rb') as f: loaded_data = pickle.load(f) print(loaded_data) ``` #### 多线程与并发编程 5. **多线程爬虫设计** 在大规模数据采集任务中,合理利用多线程技术可显著提升效率。 ```python import threading def worker(url): # 爬取逻辑 pass threads = [] urls = ['http://example.com/page{}'.format(i) for i in range(1, 6)] for url in urls: thread = threading.Thread(target=worker, args=(url,)) threads.append(thread) thread.start() for thread in threads: thread.join() ``` #### Python 进阶特性 6. **装饰器的理解与实践** 装饰器用于扩展已有函数的功能而不修改原函数定义。 ```python def my_decorator(func): def wrapper(*args, **kwargs): print("Something is happening before the function is called.") result = func(*args, **kwargs) print("Something is happening after the function is called.") return result return wrapper @my_decorator def say_hello(): print("Hello!") say_hello() ``` 7. **生成器表达式的优化** 对于大数据量迭代计算,生成器相比列表推导式更加节省内存资源。 ```python gen_expr = (i * 2 for i in range(10)) for value in gen_expr: print(value) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值