[Python-Dev] defaultdict proposal round three
John J Lee
jjl at pobox.com
Tue Feb 21 22:38:03 CET 2006
On Tue, 21 Feb 2006, Guido van Rossum wrote:
[...]
> If you're only interested in classifying the three specific built-ins
> you mention, I'd check for the presense of certain attributes:
> hasattr(x, "lower") -> x is a string of some kind; hasattr(x, "sort")
> -> x is a list; hasattr(x, "update") -> x is a dict. Also, hasattr(x,
> "union") -> x is a set; hasattr(x, "readline") -> x is a file.
dict and set instances both have an .update() method. I guess "keys" or
"items" is a better choice for testing dict-ness, if using "LBYL" at all.
(anybody new to "LBYL" can google for that and EAFP -- latter does not
stand for European Assoc. of Fish Pathologists in this context, though ;-)
> That's duck typing!
>>> hasattr(python, "quack")
True
John
More information about the Python-Dev
mailing list