[2.3] object does not appear to be a reserved word

D

Daniel Klein

Is it potentially dangerous to use the variable 'object' in code?

Thanks,
Daniel Klein
 
J

John Roth

Daniel Klein said:
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.

John Roth
 
M

Mike C. Fletcher

John said:
"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).

Enjoy,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
T

Terry Reedy

Daniel Klein said:
Is it potentially dangerous to use the variable 'object' in code?

Yes, it is potentially dangerous to use any of the names in the
builtin module. It is disrecommended for posted/public code without
good reason (intentional override).

TJR
 
D

Daniel Klein

Thanks John., that's what I suspected.

This particular module uses the variable 'object' as an argument to
several methods and functions. I'll probably change it anyway, but I
was just curious as to why it seemed to run ok.

Thanks again for the confirmation.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top