[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

is possible to get order of keyword parameters ?

rndblnch

1/25/2008 1:50:00 PM

(sorry, draft message gone to fast)

i.e. is it possible to write such a function:

def f(**kwargs):
<skipped>
return result

such as :
f(x=12, y=24) == ['x', 'y']
f(y=24, x=12) == ['y', 'x']

what i need is to get the order of the keyword parameters inside the
function.
any hints ?

thanks,

renaud
9 Answers

Marc 'BlackJack' Rintsch

1/25/2008 4:27:00 PM

0

On Fri, 25 Jan 2008 05:49:40 -0800, rndblnch wrote:

> def f(**kwargs):
> <skipped>
> return result
>
> such as :
> f(x=12, y=24) == ['x', 'y']
> f(y=24, x=12) == ['y', 'x']
>
> what i need is to get the order of the keyword parameters inside the
> function.
> any hints ?

Impossible. Dictionaries are unordered.

Ciao,
Marc 'BlackJack' Rintsch

Larry Bates

1/25/2008 4:27:00 PM

0

Keyword arguments are normally treaded as "order independent".
Why do you think you need to find the order? What problem do
you wish to solve?

-Larry



rndblnch wrote:
> (sorry, draft message gone to fast)
>
> i.e. is it possible to write such a function:
>
> def f(**kwargs):
> <skipped>
> return result
>
> such as :
> f(x=12, y=24) == ['x', 'y']
> f(y=24, x=12) == ['y', 'x']
>
> what i need is to get the order of the keyword parameters inside the
> function.
> any hints ?
>
> thanks,
>
> renaud

Duncan Booth

1/25/2008 6:40:00 PM

0

rndblnch <rndblnch@gmail.com> wrote:

> (sorry, draft message gone to fast)
>
> i.e. is it possible to write such a function:
>
> def f(**kwargs):
> <skipped>
> return result
>
> such as :
> f(x=12, y=24) == ['x', 'y']
> f(y=24, x=12) == ['y', 'x']
>
> what i need is to get the order of the keyword parameters inside the
> function.
> any hints ?
>
It would be best to do something which makes it obvious to someone reading
the function call that something magic is going on. Either get people to
pass a tuple, or if you want you could wrap a tuple in some sugar:

class _OrderedVal(object):
def __init__(self, name, current):
self._name = name
self._current = current
def __call__(self, value):
return _Ordered(self._current + ((self._name, value),))

class _Ordered(tuple):
def __init__(self, current=()):
self._current = current

def __getattr__(self, name):
return _OrderedVal(name, self._current)

ordered = _Ordered()

def f(args):
return [ k for (k,v) in args]

print f(ordered.x(12).y(24))
print f(ordered.y(24).x(12))


The question remains, what use is there for this?

rndblnch

1/25/2008 7:48:00 PM

0


On Jan 25, 5:27 pm, Larry Bates <larry.ba...@websafe.com> wrote:
> Keyword arguments are normally treaded as "order independent".
> Why do you think you need to find the order? What problem do
> you wish to solve?
>
> -Larry

On Jan 25, 7:39 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> The question remains, what use is there for this?

my goal is to implement a kind of named tuple.
idealy, it should behave like this:
p = Point(x=12, y=13)
print p.x, p.y
but what requires to keep track of the order is the unpacking:
x, y = p
i can't figure out how to produce an iterable that returns the values
in the right order.
relying on a "natural" order of the key names is not possible: x, and
y are alphabetically sorted but the following example should also
work:
size = Point(width=23, height=45)
w, h = size

if you have any suggestion to implement such a thing...
thank you

renaud

Duncan Booth

1/25/2008 8:02:00 PM

0

rndblnch <rndblnch@gmail.com> wrote:

> the following example should also
> work:
> size = Point(width=23, height=45)
> w, h = size
>

So you want the unpacking to depend on how the Point was initialised!
Aaargh!

Steven Bethard

1/25/2008 8:05:00 PM

0

rndblnch wrote:
> my goal is to implement a kind of named tuple.
> idealy, it should behave like this:
> p = Point(x=12, y=13)
> print p.x, p.y
> but what requires to keep track of the order is the unpacking:
> x, y = p
> i can't figure out how to produce an iterable that returns the values
> in the right order.
> relying on a "natural" order of the key names is not possible: x, and
> y are alphabetically sorted but the following example should also
> work:
> size = Point(width=23, height=45)
> w, h = size

There are a couple of recipes for named tuples:

http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

The latter of these will be in Python 2.6. Using that recipe, and your
example, you would write::

Point = namedtuple('Point', 'x y')
p = Point(x=12, y=13)
x, y = p

Point = namedtuple('Point', 'width', 'height')
size = Point(width=23, height=45)
w, h = size

STeVe

Steven Bethard

1/25/2008 8:06:00 PM

0

Steven Bethard wrote:
> rndblnch wrote:
>> my goal is to implement a kind of named tuple.
>> idealy, it should behave like this:
>> p = Point(x=12, y=13)
>> print p.x, p.y
>> but what requires to keep track of the order is the unpacking:
>> x, y = p
>> i can't figure out how to produce an iterable that returns the values
>> in the right order.
>> relying on a "natural" order of the key names is not possible: x, and
>> y are alphabetically sorted but the following example should also
>> work:
>> size = Point(width=23, height=45)
>> w, h = size
>
> There are a couple of recipes for named tuples:
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
> http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...
>
> The latter of these will be in Python 2.6. Using that recipe, and your
> example, you would write::
>
> Point = namedtuple('Point', 'x y')
> p = Point(x=12, y=13)
> x, y = p
>
> Point = namedtuple('Point', 'width', 'height')

Sorry, typo here. This should have read

Point = namedtuple('Point', 'width height')

> size = Point(width=23, height=45)
> w, h = size

STeVe

rndblnch

1/25/2008 8:25:00 PM

0

On Jan 25, 9:01 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> rndblnch <rndbl...@gmail.com> wrote:
> > the following example should also
> > work:
> > size = Point(width=23, height=45)
> > w, h = size
>
> So you want the unpacking to depend on how the Point was initialised!
> Aaargh!

why not?
in

def f(*args):
return iter(args)

the order of the parameters do have a semantic,
i can predict the behaviour of:

x, y = f(1, 2)

to bad it's not possible with:

def g(**kwargs):
return kwargs.itervalues()

x, y = g(x=1, y=2)

renaud

Duncan Booth

1/26/2008 12:48:00 PM

0

rndblnch <rndblnch@gmail.com> wrote:

> On Jan 25, 9:01 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
>> rndblnch <rndbl...@gmail.com> wrote:
>> > the following example should also
>> > work:
>> > size = Point(width=23, height=45)
>> > w, h = size
>>
>> So you want the unpacking to depend on how the Point was initialised!
>> Aaargh!
>
> why not?

You have to ask?

Because either you always initialise your Point with parameters in the same
order, in which case just use positional parameters which is cleaner
anyway, or when you have a function f(somePoint) you also need to know
whether it takes a Point(width,height) or a Point(height,width) and if you
get it wrong your code breaks.