is possible to get order of keyword parameters ?
Steven Bethard
steven.bethard at gmail.com
Fri Jan 25 15:05:06 EST 2008
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/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
More information about the Python-list
mailing list