Is it possible to unpack a tuple without using variables?(是否可以在不使用变量的情况下解包元组?)
问题描述
我在程序中的路径上使用 os.path.split() 函数来获取文件的文件名和路径名,然后将它们传递给另一个方法,但我目前的解决方案似乎相当难看:
I'm using the os.path.split() function on a path in my program to get the filename and pathname of a file then passing them into another method, but my current solution seems rather ugly:
path = os.path.split(somefile)
some_class(path[0], path[1])
是否可以在调用 some_class 时以更简洁的方式解包路径元组?比如:
Is it possible to unpack the path tuple in a cleaner way within the call to some_class? Something like:
some_class(os.path.split(somefile).unpack())
或者我应该以另一种方式来解决这个问题?也许是一种更 Pythonic 的方式?
Or should I simply be going about this another way? Maybe a more pythonic way?
推荐答案
是的,Python 有 参数列表解包.试试这个:
Yes, Python has argument list unpacking. Try this:
some_class(*os.path.split(somefile))
这篇关于是否可以在不使用变量的情况下解包元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以在不使用变量的情况下解包元组?


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01