is possible to get order of keyword parameters ?

R

rndblnch

(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
 
M

Marc 'BlackJack' Rintsch

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
 
L

Larry Bates

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
 
D

Duncan Booth

rndblnch said:
(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?
 
R

rndblnch

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

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
 
D

Duncan Booth

rndblnch said:
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!
 
S

Steven Bethard

rndblnch said:
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/Recipe/502237
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/500261

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
 
S

Steven Bethard

Steven said:
There are a couple of recipes for named tuples:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/502237
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/500261

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
 
R

rndblnch

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
 
D

Duncan Booth

rndblnch said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top