Anyone ever overridden a builtin by accident?

John Roth newsgroups at jhrothjr.com
Sun Sep 7 07:21:40 EDT 2003


"John Ladasky" <ladasky at my-deja.com> wrote in message
news:c09b237b.0309061922.10d74b39 at posting.google.com...
> Hi there,
>
> Just wanted to share a frustrating programming bug that, when I
> figured it out, made me gasp, and then laugh.
>
> In one of my programs I wrote...
>
> c = max(a,b)
>
> ...and I was getting the most annoying, frustrating error message:
> "type 'int' is not callable."
>
> What the heck?  I wasn't calling an integer, I was calling the
> __builtin__ function, max()!
>
> I dropped out of my regular editor (SciTE), opened IDLE, and tried
> typing in bits of my code, including the call to max().  Everything
> seemed to work fine.
>
> Then, I finally spotted the problem.  At the beginning of my program I
> had defined a variable called "max"!  I had overridden the __builtin__
> function by mistake.
>
> Is there ever a good reason to override something in __builtin__?
> It's powerful, but potentially quite confusing.  Can the interpreter
> be instructed to give a warning message when you do it?

I believe that that's in the plan somewhere. The difficulty is that
it would undoubtedly break a lot of existing code, so it isn't going
to show up in 2.4 in its full glory. A warning might be feasible,
though.

There's a program (pychecker? I think that's the name) that should
be able to tell you whether you accidentally shadowed a built-in.

> Yes indeed, *everything* in Python is an object.  Let the newbie
> beware!

John Roth
>
> --
> John J. Ladasky Jr., Ph.D.
> Department of Biology
> Johns Hopkins University
> Baltimore MD 21218
> USA
> Earth






More information about the Python-list mailing list