tags:

views:

55

answers:

2

Hi, I have a function that looks like this:

def getColumn(self, name, type, **args):

Now I would like to have a function

def getColumns(self, columns)

where I can pass a tuple that contains a tuple, where each entry in that tuple represents the arguments to be passed go getColumn. However, I'd like not to have to use named parameters for the name and type.

So I'd like not to have to use it like this:

def getColumns(self, columns):
    columns = [self.getColumn(**data) for data in columns]

column_data = ( {'name' : 'myName', 'type' : 'myType', 'other' : 'myOther'},
    {'name' : 'myName2', 'type' : 'myType2', 'other' : 'myOther2'})

How do I get rid of the 'name' : and 'type' :

Thanks in advance

+1  A: 
def getColumns(self, columns):
    columns = [self.getColumn(*data) for data in columns]

column_data = (('myName', 'myType', 'myOther'), 
               ('myName2', 'myType2', 'myOther2'))
gnibbler
+2  A: 

Something like this?

column_data = (('myName', 'myType', {'other': 'myOther'}),
               ('myName2', 'myType2', {'other2': 'myOther2'}))

def getColumn(self,name,type,**rest):
    pass

def getColumns(self,columns):
    return [self.getColumn(name, type, **rest) for name, type, rest in columns]

You don't have to unpack - if you remove ** before restin both places the function will take a dictionary.

If you simply don't want to give names to arguments, use *args instead of **kwargs as gnibbler suggested.

sdcvvc
thanks, exactly what I was looking for.Yes, I do need named parameters^^Now there wouldn't be any option to get rid of {} if there are no named arguments, would there?Thanks a lot!
phant0m
@phant0m: A very ugly way is `return [getColumn(t[0],t[1],**(t[2] if len(t)>2 else {})) for t in columns]`. It's better to leave those {} IMO.
sdcvvc
hehe ok, I'll leave it then..thanks anyway^^
phant0m