For instance:
i = 1
type = 'obj'
type(i)
will result in a "is not callable" TypeError, which may not be that clear at first glance.
To get a builtin back even though it's been overloaded, just specify the builtin namespace, as shown below:
i = 1
type = 'obj'
__builtins__.type(i)
Also, if you del the local 'type' variable, the builtin 'type' will be reinstated. After that, built-ins themselves cannot be deleted, and you will get a NameError if you try.
However, it's probably best to just avoid overloading builtins by convention. For instance, I try to remember to name a generic string variable 'astr' rather than 'str'.
1 comment:
I don't use str as a variable name so much, but I often use set, dict, and list as local variables. Often without realising that I am overriding a builtin.
Post a Comment