ordered keywords?

Ron Adam rrr at ronadam.com
Mon Oct 17 17:36:16 EDT 2005


Diez B. Roggisch wrote:
> Ron Adam wrote:
> 
>>
>> Is there a way to preserve or capture the order which keywords are given?
>>
>>  >>> def foo(**kwds):
>> ...    print kwds
>> ...
>>  >>> foo(one=1, two=2, three=3)
>> {'three': 3, 'two': 2, 'one': 1}
>>
>>
>> I would love to reverse the *args, and **kwds as well so I can  use 
>> kwds to set defaults and initiate values and args to set the order of 
>> expressions.
>>
>>
>> def foo(**__dict__, *args):
>>       print args
>>
>> print foo(x=10, y=20, x, y, x+y)
>> [10, 20, 30]
> 
> 
> This is not simply about reversing the order of kwargs and args - this 
> would require that keyword args would create bindings to variable names 
> in the scope of the caller. Which is an enterily different beast. And a 
> major semantic change in python, so it's not possible or at least not 
> happening before Python 3K.
> 
> Regards,
> 
> Diez

Yes, I kind of thought it would be pretty major.  What I'm asking is how 
to get anything close to this behavior in the current Python 2.4 now 
either with functions or with class's.  Or a way to generalize it in a 
nested data structure.  But that runs into similar problems of self 
reference.

I think the idea of putting the initialization first fits a lot of data 
patterns.  But I really don't know how the best way to implement that 
would be.

I was hoping it just might be possible (for P3k?) to have an argument 
list look to itself before looking to the caller, and then to globals 
after that.  And not create additional binding in the caller name space. 
  I think that would mean pre-creating the callee name space so it can 
be accessed by later terms in the argument list before control and the 
name space is handed over to the function.  I think this would be 
earlier evaluation instead of the late evaluation some want.


<clipped examples of possible stuff and jump to the interesting part>


def lamb(args):
     for v in args: print v

def feedlamb():
     print locals()
     lamb( (lambda x=10, y=20: (x,y,x+y))() )
     print locals()

feedlamb()

{}
10
20
30
{}


AND... (!?)


def lamb(args):
     for v in args: print v

def feedlamb():
     print locals()
     y = 20
     lamb( (lambda x=10: (x,y,x+y))() )
     print locals()

feedlamb()

{}
10
20
30
{'y': 20}


Cool, this is the exact behavior I was thinking of, but without the 
lambda if possible, and without all the extra parentheses.  Anyway to 
clean this up?

Maybe it wouldn't be as big a change as it seems?  ;-)

Cheers,
    Ron







More information about the Python-list mailing list