How can an int be '+' with a tuple?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 3 01:08:08 EDT 2018


On Sun, 03 Jun 2018 04:59:34 +0000, Steven D'Aprano wrote:

> On Sun, 03 Jun 2018 10:55:04 +0800, Jach Fong wrote:
> 
>> The attached is a script which can run under Python 3.4/Windows Vista
>> correctly. One thing make me puzzled is that the "any + context" at
>> line 18. The "any" was passed as an integer from line 43 and the
>> "context" was defined as a tuple at line 35. This concatenation works!
>> how?
> 
> 90% of the script you attached is irrelevant to your question. None of
> the tkinter or threading code is important. The only important parts
> are:
> 
> def threaded(action, args, context, onExit, onProgress):
>     def progress(*any):
>         threadQueue.put((onProgress, any + context))
> 
> Here we can tell that ``any`` is a tuple. 

Oops, I misread your question. You thought any was an int. But the * 
(star) notation in function parameters makes the parameter collect any 
unnamed arguments into a single tuple.

def test(first, *args):
    print(args)

test(1, 2, 3, "hello", 99)
=> prints the tuple (2, 3, "hello", 99)



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list