Skip to content Skip to sidebar Skip to footer

Sub-classing A Built-in Python Type Such As Int

I tried sub-classing the built-in int object along the lines of this example but I got a strange error. Here is the code and resulting Traceback. # Built-in namespace import __buil

Solution 1:

You can subclass a builtin type, the problem comes with you try to replace a built-in type. This is a chicken and egg problem.

As a golden rule, never replace builtins. If you never want to use an int again in YOUR PROGRAM, then do int = myint and call it good. Don't touch __builtin__.int

The specific error here is IPython doing some validation and checking if type(foo) is int, which fails. If they'd done isinstance(foo, int) it would have succeeded. Be a conscientious coder -- don't muck with the internals that other modules are using.

Post a Comment for "Sub-classing A Built-in Python Type Such As Int"