Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Using tuple as parameter to a function Date: Mon, 09 Nov 2015 16:01:14 +0100 Organization: Decebal Computing Lines: 54 Message-ID: <87egfz5i9x.fsf@Equus.decebal.nl> References: <87io5b5m1b.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oCJpywr+ufz1ws4GadIen2A8prLRyA/E=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:75cv4GrVTA/eDV8A6yCSETYclR0= sha1:UcAVmJI2J8X01PMzpJYyAEXfoS8= Xref: csiph.com comp.lang.python:98534 On Monday 9 Nov 2015 14:58 CET, Chris Angelico wrote: > On Tue, Nov 10, 2015 at 12:40 AM, Cecil Westerhof wrote: >> I was thinking about something like: >> values = (( 1, 100), ( 2, 100), ( 5, 100), >> 10, 100), (20, 100), (40, 100)) >> for value in values: >> do_stress_test('sqlite', ???) >> do_stress_test('postgres', ???) >> >> Is this possible? If so: what do I put at the place of the '???'? >> >> I could change the second and third parameter to a tuple as the >> second parameter, but I prefer three parameters if that would be >> possible. > > Easy! Just unpack the tuple. Two options: > > # Unpack in the loop > for count, size in values: > do_stress_test('sqlite', count, size) > do_stress_test('postgres', count, size) > > # Unpack in the function call > for value in values: > do_stress_test('sqlite', *value) > do_stress_test('postgres', *value) > > Either will work. For what you're doing here, I'd be inclined to the > first option, so you can give the values appropriate names (I'm > completely guessing here that they might be some sort of iteration > count and pool size; use names that make sense to your program); the > other option looks uglier in this particular instance, though it's a > more direct answer to your question. > > This is one of Python's best-kept secrets, I think. It's not easy to > stumble on it, but it's so handy once you know about it. I remembered the second one, but did not know how it was done. That was why I asked about it. But the first option is even better. I now use: values = (( 1, 100), ( 2, 100), ( 5, 100), ( 10, 100), ( 20, 100), ( 40, 100), #( 80, 100), (160, 100), #(160, 200), (160, 400), (160, 800), ) for no_of_threads, no_of_records in values: do_stress_test('postgres', no_of_threads, no_of_records) do_stress_test('sqlite', no_of_threads, no_of_records) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof