473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Anyone ever overridden a builtin by accident?

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?

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

--
John J. Ladasky Jr., Ph.D.
Department of Biology
Johns Hopkins University
Baltimore MD 21218
USA
Earth
Jul 18 '05 #1
5 2893
|I had overridden the __builtin__ function by mistake.

I had a script or two that used 'dict' as a variable name... which was
fine, until Python 2.2. Since then, I've used 'dct' for the same
purpose. But possibly something old is still sitting around with the
problem.

Yours, Lulu...

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons. Intellectual
property is to the 21st century what the slave trade was to the 16th.

Jul 18 '05 #2
In article <c0**************************@posting.google.com >, John
Ladasky <la*****@my-deja.com> writes
Hi there,

Just wanted to share a frustrating programming bug that, when I
figured it out, made me gasp, and then laugh. .......
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'm sure there are reasons why builtin classes/objects/methods might
need to be overridden, but I think it should perhaps be a bit more
difficult.

I believe there is an intention to make this harder than it currently
is. There is some effort made into making shadowing of module globals
hard (to allow some code optimisations I guess) builtins are also sort
of global so perhaps the same restrictions should apply.
Yes indeed, *everything* in Python is an object. Let the newbie
beware!

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


--
Robin Becker
Jul 18 '05 #3

"John Ladasky" <la*****@my-deja.com> wrote in message
news:c0**************************@posting.google.c om...
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

Jul 18 '05 #4
> Yes indeed, *everything* in Python is an object. Let the newbie
beware!


Common Lisp solves this problem in a pretty simple way - functions
and variables are in separate namespaces (of course scheme programmers
don't like this very much IIRC) and all global variables are commonly
named *booo* *bah* and etc. so if you are overriding a global variable
you are at least aware of it ...
Ignas Mikalajunas
Jul 18 '05 #5

la*****@my-deja.com (John Ladasky) writes:
In one of my programs I wrote...

c = max(a,b) 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.

vo****@centras.lt (Ignas Mikalajunas) writes:
Common Lisp solves this problem in a pretty simple way - functions
and variables are in separate namespaces
But it doesn't really _solve_ the problem, does it? It solves the
problem of overriding function bindings with data bindings (and vice
versa), but does nothing to prevent overriding function bindings with
other function bindings. Consider:

def list():
print "list"

and the Common Lisp equivalent

(defun list ()
(print "list"))

In both cases you're clobbering a built-in. The difference, however, is
that, in Python you've merely shadowed the built-in -- the original is
still there in Python's outermost scope (builtins), while in CL you
are attempting to replace the built-in[*].

Common Lisp "solves" it, by forbidding it. But I do not think that
implementations are required to enforce the rule, so some (eg clisp)
complain:

[1]> (defun list () (print "list"))

** - Continuable Error
DEFUN/DEFMACRO(LIST): #<PACKAGE COMMON-LISP> is locked
If you continue (by typing 'continue'): Ignore the lock and proceed
1. Break [2]>

while others (eg CMUCL), let it pass silently:

Loaded subsystems:
Python 1.1, target Intel x86
CLOS 18e (based on PCL September 16 92 PCL (f))
* (defun list () (print "list"))

LIST

(Python, BTW, is the name of CMUCL's compiler :-)
all global variables are commonly named *booo* *bah* and etc. so if
you are overriding a global variable you are at least aware of it ...


Although the real purpose of the asterisks is to warn you about the
fact that the variable is dynamically bound[+], and that any local
bindings that you may make for it, will also be dynamic. The asterisks
are another form of namespace separation, but they address an
orthogonal issue, and offer absolutely no protection against
clobbering global _function_ definitions; global function names have
no asterisks (because they are not dynamically bound[#]).

[*] To be fair, Lisp distinguishes global function definitions from
local ones, while in Python they are always local, so one can
argue that the CL equivalent of Python's def is really flet or
labels, which leave the built-in functions well alone, just as is
the case in Python.

[+] As opposed to lexically bound, which is the default in CL, and the
only possibility in Python.

[#] Though it has been suggested recently, that dynamic function
binding subsumes AOP ... but that's getting waaaaay off-topic.
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: dterrors | last post by:
Will php 6 do strong typing and/or namespaces? I was shocked to find out today that there are some people who actually argue that weak typing is somehow better. I didn't even know there was a...
4
by: Kim Petersen | last post by:
I've worked on this table object a bit too long - and seem to have stared too long at the code. Can someone see where it goes wrong in the insertrow function? Also optimization hints and...
1
by: Stephen Ferg | last post by:
Python has a builtin class for staticmethod. Seems to me that Python should also have a builtin class for abstractmethod. Something like this... ####################################### #...
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
6
by: garyjefferson123 | last post by:
I'm having a scoping problem. I have a module called SpecialFile, which defines: def open(fname, mode): return SpecialFile(fname, mode) class SpecialFile: def __init__(self, fname, mode):...
11
by: S. I. Becker | last post by:
Is it possible to determine if a function has been overridden by an object, when I have a pointer to that object as it's base class (which is abstract)? The reason I want to do this is that I want...
8
by: Allan Ebdrup | last post by:
I'm writing some code where I have have a class that implements 4 methods (class A) I only want to call these methods from the base class if they have been overridden in a sub class (Class B) I...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
6
by: wink | last post by:
I'd like to determine if a method has been overridden as was asked here: http://www.velocityreviews.com/forums/t564224-determining-whether-a-derived-class-overrides-a-virtual-memberfunction.html...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.