[Tutor] passing arguments to functions - problem with argument order

tetsuo2k6 at web.de tetsuo2k6 at web.de
Mon Mar 10 19:02:15 CET 2008


That's it!

Paul



Andreas Kostyrka schrieb:
> What you probably want is to pass:
> 
> writer(None, "field1", "field2")
> 
> Andreas
> 
> Am Montag, den 10.03.2008, 16:28 +0100 schrieb tetsuo2k6 at web.de:
>> And I thought I might get away without using dicts...
>>
>> Thanks, Greg
>>
>>
>>
>> Greg Graham schrieb:
>>> Paul,
>>>
>>> Python does not allow mixing variable length arguments and keyword arguments in that way. To accomplish what you want, you must add an argument preceded by a "**" which will be a dict containing all of the keyword arguments as key, value pairs. You then have to retrieve the arguments from the dict by name. When called, the keyword arguments must be last.
>>>
>>> Here is a little example:
>>>
>>> def test(*column_definitions, **options):
>>>     print "Column Definitions:" + ", ".join(column_definitions)
>>>     output_csv_filename = options.get('output_csv_filename', None)
>>>     print "Output csv filename: " + str(output_csv_filename)
>>>
>>>
>>>>>> test("kundennummer", "anrede", "vorname", "nachname", "plz", "ort", "adresse", "kontoinhaber", "blz", "kto", "bankname", "status", "spielbeginn", "letzte_aenderung", "importdatum", "briefdatum", "buchungsdatum", "stornodatum", output_csv_filename=None)
>>> Column Definitions:kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum
>>> Output csv filename: None
>>>
>>> Greg
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list