[Tutor] passing arguments to functions - problem with argument order
Greg Graham
GGraham at cistercian.org
Mon Mar 10 16:09:16 CET 2008
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
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf Of tetsuo2k6 at web.de
Sent: Monday, March 10, 2008 9:19 AM
To: python tutor
Subject: [Tutor] passing arguments to functions - problem with argument order
I don't get this - what is the clean way of the order of passing
arguments to functions?
The called function goes like this:
def csvwriter(output_csv_filename=None, *coloumn_definitions):
"""Edit Me!"""
if output_csv_filename == None:
output_csv_filename = raw_input("Name der zu
erzeugenden Datei (vollständiger Pfad)? (ACHTUNG: WIRD ÜBERSCHRIEBEN,
FALLS VORHANDEN!) ")
first_row = ";".join(coloumn_definitions)
print(first_row)
try:
file = open(output_csv_filename, "w")
file.writerow(first_row)
file.close()
except Exception, e:
print("Konnte %s nicht zum Schreiben öffnen.")
sys.exit(e)
return csv.writer(open(output_csv_filename, "ab"),
delimiter=";", quoting=csv.QUOTE_NONE)
The call to the function seems impossible to do. When I say:
writer = dgf.csvwriter(output_csv_filename=None,
"kundennummer", "anrede", "vorname", "nachname", "plz", "ort",
"adresse", "kontoinhaber", "blz", "kto", "bankname", "status",
"spielbeginn", "letzte_aenderung", "importdatum", "briefdatum",
"buchungsdatum", "stornodatum")
I get:
SyntaxError: non-keyword arg after keyword arg
-> So I guess I have to put keyword arg at the end...
When I put output_csv_writer at the end:
writer = dgf.csvwriter("kundennummer", "anrede",
"vorname", "nachname", "plz", "ort", "adresse", "kontoinhaber", "blz",
"kto", "bankname", "status", "spielbeginn", "letzte_aenderung",
"importdatum", "briefdatum", "buchungsdatum", "stornodatum",
output_csv_filename=None)
I get:
TypeError: csvwriter() got multiple values for keyword argument
'output_csv_filename'
-> Am I right that output_csv_filename now becomes "kundennummer" at first?
Also, changing the function definition gives me syntax error:
def csvwriter(*coloumn_definitions, output_csv_filename=None):
^
SyntaxError: invalid syntax
What's going on here? I'm confused...
Paul
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list