Hello,
Is it possible to add a documentation string to a namedtuple in an easy manner?
I tried
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
"""
A point in 2D space
"""
# Yet another test
"""
A(nother) point in 2D space
"""
Point2 = namedtuple("Point2", ["x", "y"])
print Point.__doc__ # -> "Point(x, y)...
Can I reference a namedtuple fieldame using a variable?
from collections import namedtuple
import random
Prize = namedtuple("Prize", ["left", "right"])
this_prize = Prize("FirstPrize", "SecondPrize")
if random.random() > .5:
choice = "left"
else:
choice = "right"
#retrieve the value of "left" or "right" depending on the ch...
I was having trouble implementing 'namedtuple._replace()', so I copied the code right off of the documentation:
Point = namedtuple('Point', 'x,y')
p = Point(x=11, y=22)
p._replace(x=33)
print p
and I got:
Point(x=11, y=22)
instead of:
Point(x=33, y=22)
as is shown in the doc.
I'm using Python 6.2 on Windows 7
edit: oops- tha...
How do I check if an object is an instance of a Named tuple?
...
Is there a "Rosetta stone" which shows how to use Python tables
of namedtuples in sqlite3 ?
Say we have a namedtuple Row and a list or dict rows:
Row = namedtuple( "Row", "name city x y" )
rows = [None, Row( "Jones", "Boston", 3, 4 ), Row(...) ... ]
# translate the following to sqlite please --
rows[id]
rows[id] = Row(...)
rows.ap...
How can namedtuples be extended or subclassed with many additional @properties ?
For a few, one can just write the text below; but there are many,
so I'm looking for a generator or property factory.
One way would be to generate text from _fields and exec it;
another would be an add_fields with the same effect at runtime.
(My @props are t...
Take this code:
>>> import urlparse
>>> parts = urlparse.urlparse('http://docs.python.org/library/')
>>> parts = parts._replace(path='/3.0'+parts.path)
parts._replace works but as it is an underscored method, it's supposed to be internal, and not used. Is there an alternative? I don't want to do:
>>> parts = parts[:2] + ('/3.0'+parts...
I am very new to Python, and trying to figure out how to create an object that has values that are accessible either by attribute name, or by index. For example, the way os.stat() returns a stat_result or pwd.getpwnam() returns a struct_passwd.
In trying to figure it out, I've only come across C implementations of the above types. No...
Reading the changes in Python 3.1, I found something... unexpected:
The sys.version_info tuple is now a named tuple:
I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I never expected they could be indexed both ways.
Thus, my qu...
I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working:
class Status(collections.namedtuple("Status", "started checking start_after_check checked error paused queued loade...