Iterators from urllib2
Andrew Dalke
dalke at dalkescientific.com
Fri Jul 22 13:41:47 EDT 2005
Joshua Ginsberg wrote:
> >>> dir(ifs)
> ['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close',
> 'fileno', 'fp', 'geturl', 'headers', 'info', 'next', 'read',
> 'readline', 'readlines', 'url']
>
> Yep. But what about in my code? I modify my code to print dir(ifs)
> before creating the DictReader...
>
> ['__doc__', '__init__', '__module__', '__repr__', 'close', 'fp',
> 'geturl', 'headers', 'info', 'read', 'readline', 'url']
...
> Whoa! Where did the __iter__, readlines, and next attributes
> go? Ideas?
That difference comes from this code in urllib.py:addbase
class addbase:
"""Base class for addinfo and addclosehook."""
def __init__(self, fp):
self.fp = fp
self.read = self.fp.read
self.readline = self.fp.readline
if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines
if hasattr(self.fp, "fileno"): self.fileno = self.fp.fileno
if hasattr(self.fp, "__iter__"):
self.__iter__ = self.fp.__iter__
if hasattr(self.fp, "next"):
self.next = self.fp.next
It looks like the fp for your latter code
doesn't have the additional properties. Try
adding the following debug code to figure out
what's up
print dir(ifs)
print "fp=", ifs.fp
print "dir(fp)", dir(ifs.fp)
Odds are you'll get different results.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list