#pragma section-numbers off == See Also == * SubclassingDictionaries = Discussion = I wonder a lot, "How do I subclass built-in types?" There's a related question: "How do I figure out if something is ''sort of'' like a particular type?" My goal is to take something, and say, "Does this act pretty much like an integer?" Or "does this act pretty much like a float?" Or "does this act pretty much like a dictionary?" You could: count up all the behaviors you rely on. Then test if they all exist. If they all exist, consider that item a fit. But, that seems really hard, if you've got a whole class with a ton of methods, all of which use the item. "Counting up" seems pretty hard in that case, and it seems like it would require a lot of discipline. Is there, then, a set of tests that we can run over an item, to see if it acts "pretty much like" an int, a float, a dict, a string, blah blah blah...? (Or, at least supports the interface for all those things?) What do people do about this? Because I read all these things saying, "Don't use {{{type}}}!" "Don't use {{{isInstance}}}!" But I don't see much in the way of what ''to'' use. -- LionKimbro <> ---- [lwickjr]: I like to use {{{issubclass}}}. I suppose one could define a module in the Standard Library consisting entirely of empty classes with appropriate names, and inherit from them: Module interfaces:{{{ ... class dictLike: pass ... }}} User module: {{{ import interfaces ... class Shelf(interfaces.dictLike,...): ... }}} Then other users could write {{{isinstance(Thing, interfaces.dictLike)}}}, and be assured that anything that passes the test /promises/ to be "like a dictionary". Comments? ---- For instance, on the {{{int}}} class, there are some gazillion methods: #!python >>> dir( 42 ) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__'] >>> }}} If you want to make something that's functionally ''like'' an int, can you get around having to implement all these methods? Maybe I have the wrong page title; Maybe this shouldn't be called "SubclassingBuiltInTypes," but rather "SimulatingBuiltInTypes." -- LionKimbro <>