Namedtuples: TypeError: 'str' object is not callable

Deborah Swanson python at deborahswanson.net
Fri Jan 6 22:46:50 EST 2017


I'm not sure what Python is complaining about here, or why.
 
Here's the example from the Python docs: 
https://docs.python.org/3/library/collections.html#collections.namedtupl
e

EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title,
department, paygrade')

import csv
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv",
"rb"))):
    print(emp.name, emp.title)

And here's my code:

listings = namedtuple('listings', ['CLDesc', 'url', 'desc',
'Description', 	'Location', 'STco', 'miles', 'Kind', 'Rent', 'Date',
'br', 'Notes', 
	'yesno', 'mark', 'arc'])
for lst in map(listings._make, csv.reader(open('Moving 2017.csv',
"r"))):
	.
	.

(['x', 'y'] is a valid format for the field names, and there are no
errors when the definition for listings executes.)


And here's the Traceback in PyCharm:
  File "E:/Coding projects/Pycharm/Moving/moving_numberedtuples.py",
line 139, in moving()
    for lst in map(listings._make, csv.reader(open('E:\\Coding
projects\\Pycharm\\Moving\\Moving 2017 in.csv',"r"))):
TypeError: 'str' object is not callable

What str object is not callable, and how do I fix it?  I see an
iteration variable (lst), a namedtuples method call(listings._make,
which makes a new instance of listings) and a csv.reader call, a
variable and 2 method calls, none of which are bare strings, except
possibly 'listings._make', but there's no str it might be trying to call
that I can see. My 'for' statement looks like a good copy of the example
from the docs to me, and the example in the docs uses
'EmployeeRecord._make' with no parentheses.

Is the example from the docs possibly in error? I don't see any other
possibility, but maybe I'm not looking in the right place. 

I don't know enough about classes in Python yet to know if this is ok or
not. And when I tried again with 'listings._make()', I got:

TypeError: _make() missing 1 required positional argument: 'iterable'.

This suggests that maybe 'listings._make' is wrong, but if it should be
'listings._make()', what should the iterable be? 'listings'?

'listings._make(listings)' looks awfully redundant for Python, so 
'_make(listings)' would make more sense (pun not intended), and seems to
be what the error thinks it is. But when I change it to
'_make(listings)', I get:

NameError: name '_make' is not defined

'listings._make(listings)' gets "TypeError: 'type' object is not
iterable", which answers the question of whether a namedtuple instance
(if that's what 'listings' is) is an iterable. It isn't.

There might be a version problem here. I'm using Python 3.4.3, and the
url for this page simply indicates version 3. As most of us know,
there's a lot of variance between dot versions of 3, and quite possibly
not all of the changes are documented correctly. In 3.4.3 the usage
might be slightly different, or the code broken.

Any help with this would be hugely appreciated.




More information about the Python-list mailing list