Is it possible to unpack a tuple without using variables?

情到浓时终转凉″ 提交于 2019-12-04 02:56:17

问题


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])

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())

Or should I simply be going about this another way? Maybe a more pythonic way?


回答1:


Yes, Python has argument list unpacking. Try this:

some_class(*os.path.split(somefile))


来源:https://stackoverflow.com/questions/1812020/is-it-possible-to-unpack-a-tuple-without-using-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!