tags:

views:

1498

answers:

9

Why is it bad to name a variable 'id' in Python?

+5  A: 

Because it's the name of a builtin function.

ahockley
+1  A: 

Because id is a built in function

Jacob
+2  A: 

It's bad to name any variable after a built in function. One of the reasons is because it can be confusing to a reader that doesn't know the name is overridden.

Toni Ruža
+1  A: 

'id' is a built-in method in Python. Assigning a value to 'id' will overwrite the method. It is best to use either an identifier before as in "some_id" or use it in a different capitalization method.

The built in method takes a single parameter and returns an integer for the memory address of the object that you passed.

>>>id(1)

9787760

>>>x = 1

>>>id(x)

9787760

brian buck
note that you can name a class attribute or method 'id', that won't touch the built in function.
Toni Ruža
+20  A: 

"id()" is a fundamental built-in:

Help on built-in function id in module builtin:

id(...) id(object) -> integer

Return the identity of an object.  This is guaranteed to be unique among
simultaneously existing objects.  (Hint: it's the object's memory
address.)

In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.

-k

Kevin Little
"id" is removed as a builtin from Python 3.0 and has been moved to a module in the standard library. So this advice will no longer be true once people are using 3.0
Eli Courtwright
OK, for 'id' you're right, but the "in general..." comment still applies, don't you think?
Kevin Little
I would avoid it for module globals certainly. For variables restricted to local scope, so you can see the same function isn't ever going to need to use the builtin, it's not something I'd worry about.
bobince
+12  A: 

I might say something unpopular here: id() is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, where it's clear that id doesn't mean the built-in function.

Sebastian Rittau
I would still avoid it if at all possible though. Even if for no other reason than to not hear coworkers complain. :-)
Jason Baker
A: 

Because python is a dynamic language, it's not usually a good idea to give a variable and a function the same name. id() is a function in python, so it's recommend not to use a variable named id. Bearing that in mind, that applies to all functions that you might use... a variable shouldn't have the same name as a function.

Kitty
+11  A: 

id is a built-in function that gives the memory address of an object. If you name one of your functions id, you will have to say __builtins__.id to get the original. Renaming id globally is confusing in anything but a small script.

However, reusing built-in names as variables isn't all that bad as long as the use is local. Python has a lot of built-in functions that (1) have common names and (2) you will not use much anyway. Using these as local variables or as members of an object is OK because it's obvious from context what you're doing:

Example:

def numbered(filename):
  file = open(filename)
  for i,input in enumerate(file):
    print "%s:\t%s" % (i,input)
  file.close()

Some built-ins with tempting names:

  • id
  • file
  • list
  • map
  • all, any
  • complex
  • dir
  • input
  • slice
  • buffer
Nathan Sanders
A: 

In response to:

id is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, whre it's clear that id doesn't mean the built-in function.

While this is true, it's probably a good idea to be more specific with this variable name than simply "id". Lots of things have IDs (especially if you're working with a RDBMS), and as the second line of Tim Peters's The Zen of Python tells us:

Explicit is better than implicit.

See the rest by running: import this

Ross