Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass-through keyword arguments

Tags:

python

I've got a class function that needs to "pass through" a particular keyword argument:

def createOrOpenTable(self, tableName, schema, asType=Table):
    if self.tableExists(tableName):
        return self.openTable(tableName, asType=asType)
    else:
        return self.createTable(self, tableName, schema, asType=asType)

When I call it, I get an error like this:

TypeError: createTable() got multiple values for keyword argument 'asType'

Is there any way to "pass through" such a keyword argument?

I've thought of several answers, but none of them are optimal. From worst to best:

  • I could change the keyword name on one or more of the functions, but I want to use the same keyword for all three functions, since the parameter carries the same meaning.

  • I could pass the asType parameter by position instead of by keyword, but if I add other keyword parameters to openTable or createTable, I'd have to remember to change the calls. I'd rather it automatically adapt, as it would if I could use the keyword form.

  • I could use the **args form here instead, to get a dictionary of keyword parameters rather than using a default parameter, but that seems like using a sledgehammer to swat a fly (because of the extra lines of code needed to properly parse it).

Is there a better solution?

like image 498
Head Geek Avatar asked Jun 19 '09 14:06

Head Geek


1 Answers

You're doing it right... Just take out the self in the second function call :)

  return self.createTable(self, tableName, schema, asType=asType)

should be:

  return self.createTable(tableName, schema, asType=asType)
like image 96
David Wolever Avatar answered Sep 28 '22 09:09

David Wolever