[2.3] object does not appear to be a reserved word
Mike C. Fletcher
mcfletch at rogers.com
Sat Sep 6 22:10:13 CEST 2003
John Roth wrote:
>"Daniel Klein" <danielk at aracnet.com> wrote in message
>news:v89klvsupdoe39e6trqbbqvmhfgecnoum8 at 4ax.com...
>>Is it potentially dangerous to use the variable 'object' in code?
>"object" is, I believe, a built-in type in the builtins namespace.
>As such, you can shadow it so it's inacessable. However,
>the only place were that could cause problems is if you
>use it as an identifier at the module level. I think.
Another place: errors in (copied) code which get silently ignored
because of the presence of the "object" type.
For instance, if you cut-and-past a few lines of (older, particularly
pre-2.2) code which uses the variable "object" into a new situation
which uses "item" (or whatever), the older code will often seem to be
functioning properly because it uses the type "object" as an instance,
instead of raising a NameError. Tends to result in subtle bugs, but not
likely enough to warrant breaking all the old code that used "object" as
an identifier by making it a keyword.
To answer the original question, yes, it's somewhat dangerous to use
"object" as an identifier in code. It's approximately equally dangerous
to use "str", "list", or "tuple" as an identifier. It's just that the
temptation to use "object" is greater when you're doing a lot of
polymorphic programming, and you'll find older code (pre 2.2) using it
without any concern whatsoever (because there was no object type back
then to cause a problem).
Each new built-in causes the same problem, it's just that "object" is so
fundamental an idea (both as a built-in and an identifer) that it's
probably the most likely source of such low-level conflicts going
forward. (e.g. enumerate, or zip are used in code once in a while, but
compared to "object" are fairly uncommon choices for an identifier).
Mike C. Fletcher
Designer, VR Plumber, Coder
More information about the Python-list