[Types-sig] File Protocol Proposal
Barry A. Warsaw
barry@digicool.com
Wed, 14 Mar 2001 12:50:37 -0500
| def isatty():
| return false
Two thoughts of the day...
We may need to add a boolean type to Python. How would you describe
today a method that takes a true/false flag? There's a lot of variety
in how you can spell true and false today.
Writing a boolean class isn't hard -- I've done it at least twice
<wink>. Writing a boolean type was more difficult because of the old
comparison rules; I haven't tried since richcomps have been added so
the situation might be better now.
There's also the common situation of accepting either None or a
specific object. I.e. "this method takes None or a Foobar instance".
And what about a method that might accept any of None, a string
(filename), or a "file-like" object? This is all very common in
Python programs.
Finally, another thought occurred to me, which might be based on a
mis-remembering of what ObjC does (I don't have any of my old ObjC
books laying around, and it's been a while).
Say a method takes an object that must have a read([size]) method.
That's all you care about. Yes, you could define an interface that
describes this, but what if someone passes in an object that conforms
to a full blown "ReadableFile" interface -- i.e. it provides
read([size]), readline(), readlines(), and maybe seek()? Let's say
those two interfaces are defined in unrelated and separately authored
libraries, but I'm writing code that combines the two. Library B
gives me a factory that produces ReadableFiles and I'd like to pass
those to library B's method that specifies a MustHaveRead interface.
Am I screwed? Or do I have to play convoluted games of multiple
inheritance, mixins, interface mishmashing, etc? To a newbie (and
maybe a not-so-newbie), it ought to be perfectly legal.
ReadableFile's interface is a superset of MustHaveRead so it should be
fine, but because the interface objects aren't explicitly related,
you're hosed.
What you want (maybe) is interface conformance based on a runtime
check of the interface specifications. Maybe a kind of interface
arithmetic? IIRC, this was one of the ObjC uses of categories. A
category is just a collection of methods, so if you have a category
that contained only read([size]), and you declared a parameter to be
of the "type" of that category, you could pass in any object that had
such a method.
Well, back to peripherally following this list. :)
-Barry