[Python-3000] Builtin iterator type

Michael Urman murman at gmail.com
Mon Nov 20 15:25:27 CET 2006


> However, of you know ahead of time that not all birds can fly you can
> design for this.

To use a more relevant example, how about file-like-objects and
nameless files. Any class derived from file can be expected to have
the name member. However several files have nonsense names:

>>> f = tempfile.TemporaryFile()
>>> isinstance(f, file)
True
>>> f.name, sys.stdin.name, sys.stdout.name
('<fdopen>', '<stdin>', '<stdout>')

Furthermore arbitrary file-like objects may or may not see it as
necessary to provide a name attribute:

>>> f = urllib.urlopen('http://www.google.com')
>>> isinstance(f, file)
False
>>> f.read(62)
'<html><head><meta http-equiv="content-type" content="text/html'
>>> f.name
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: addinfourl instance has no attribute 'name'

I'm skeptical that class hierarchy design time is a good time to
target. Class hierarchies should be relatively rare in python,
maintenance is more common, and developers new to your class hierarchy
not knowing the special cases are likely to make incorrect
assumptions. Both of the following are wrong, but are easy assumptions
to have made:

  1) All file-like objects have a usable name
  2) All file-like objects have a name attribute

To tie this back to the duck vs. lawyer context, if a function created
by someone else is going to read data from a file object, which would
make it easiest for you to use in legitimate ways the other programmer
hadn't planned?

  1) It checks for isinstance(obj, file),
  2) It opens a copy with file(obj.name, 'r'),
  3) It allows filenames by opening file(obj, 'r'),
  4) It allows filedata by creating a StringIO(obj),
  5) It requires the (fuzzy) right thing and calls obj.read(), or
  6) It does 5 with a fallback of 4 or 3 if it can't find obj.read.

I've set this scenario to make 6 the obvious answer, but it should be
clear that there isn't a single winner between 3 and 4 across all
scenarios.
-- 
Michael Urman  http://www.tortall.net/mu/blog


More information about the Python-3000 mailing list