[Baypiggies] Newbie-ish type questions...
Charles Merriam
charles.merriam at gmail.com
Wed Apr 23 19:20:20 CEST 2008
Thanks for the help, saying I'm not crazy :)
Kelly: I agree on letting duck-typing do its thing. Most of the type
checking shows up in my testing code, along the lines of "I really did
get back a hash with values being an array of five arrays of strings".
Keith: Unfortunately, the code is different for different tests of
types. For example, "if type(l) is list" but if hasattr(i,
"__iter__")... The use of function calls was to regularize the
interface. The types module only defines nice words for the various
types, including things like 'code' or 'function' which take a couple
extra lines. It doesn't simplify tests.
Paul: I think you're right; at least it agrees with what I've read of
ABCs. It means that all the tests of standard types become
is_instance tests. Good. It also looks like any arbitrary interface
can be described by overriding __instancecheck__. Are there example
for "This takes an interface of (open(), read(), close()) so I return
true if you ask isInstance(ReadableFile)
Chad: Thanks for mentioning the tests hiding in the operator class.
Just the thing I was searching for in the types module. Ah, random
growth.
Paul: FYI, also "strings" are sequences, "sets" are not.
Thanks again,
Charles
More information about the Baypiggies
mailing list