0

When I try to print Link.votes I get <property object at 0x1027b4f18> returned when I was expecting the value for "votes" (100 in the example below). Can someone please let me know what I'm doing wrong?

from collections import namedtuple

Link = namedtuple('Link', ['id', 'country_id', 'date', 'votes', 'url'])
Link(0, "US", 111105, 100,"http://www.google.com")
print Link.votes
Rico
  • 58,485
  • 12
  • 111
  • 141
Andy
  • 275
  • 2
  • 14
  • The first google result for "python namedtuple" was the official Python documentation. If you check the namedtuple section, you'll get all the info you need http://docs.python.org/2/library/collections.html#collections.namedtuple –  Jan 31 '14 at 02:28
  • Also see http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python –  Jan 31 '14 at 02:28

1 Answers1

7

You need to create a new Link object. When you write Link = namedtuple(...), you are creating a new class called Link. Then, when you write Link(...), this instantiates a Link object, whose .votes property you can then access.

from collections import namedtuple
Link = namedtuple('Link', ['id', 'country_id', 'date', 'votes', 'url'])
mylink = Link(0, "US", 111105, 100, "http://www.google.com")
print mylink.votes

Result:

100
senshin
  • 10,022
  • 7
  • 46
  • 59