Supply condition in function call

Peter Otten __peter__ at web.de
Thu Mar 26 06:37:48 EDT 2015


Cameron Simpson wrote:

> On 26Mar2015 10:03, Peter Otten <__peter__ at web.de> wrote:
>>Cameron Simpson wrote:
>>>       vars = locals()
>>>       varnames = list(vars.keys())
>>
>>That leaves varnames in undefined order. Consider
>>
>>varnames = sorted(vars)
> 
> Actually, not necessary.
> 
> I started with sorted, but it is irrelevant, so I backed off to "list" to
> avoid introducing an unwarranted implication, in fact precisely the
> implicaion you are making.
> 
> The only requirement, which I mentioned, is that the values used to
> initialise the namedtuple are supplied in the same order as the tuple
> field names, so all that is needed is to suck the .keys() out once and use
> them in the same order when we construct the namedtuple. Hence just a
> list.

You are right. 

Once I spotted the "error" I failed to notice that you pass the named tuple 
as a single argument, i. e. condition(nt), not condition(*nt) :(

By the way, in this case you don't need the list at all:

def vartuple(vars):
    return namedtuple("locals", vars)._make(vars.values())





More information about the Python-list mailing list