开发者

Is it possible to unpack a tuple without using variables?

开发者 https://www.devze.com 2022-12-12 22:31 出处:网络
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:

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 s开发者_Go百科ome_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?


Yes, Python has argument list unpacking. Try this:

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

精彩评论

暂无评论...
验证码 换一张
取 消