Revision 3 as of 2004-09-13 00:00:50

Clear message

See Also

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 DateTime(2004-09-12T17:10:47Z)

For instance, on the int class, there are some gazillion methods:

   1 >>> dir( 42 )
   2 ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__',
   3 '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__',
   4 '__getnewargs__', '__hash__', '__hex__', '__init__', '__int__', '__invert__', '__long__',
   5  '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__',
   6 '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__',
   7 '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__',
   8 '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
   9 '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__',
  10 '__xor__']
  11 >>> 

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 DateTime(2004-09-13T00:00:50Z)

Unable to edit the page? See the FrontPage for instructions.