Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `id` a keyword in python?

Tags:

python

keyword

My editor (TextMate) shows id in another colour (when used as variable name) than my usual variable names. Is it a keyword? I don't want to shade any keyword...

like image 884
Aufwind Avatar asked Jun 14 '11 22:06

Aufwind


People also ask

What is id in Python?

Python id() Function All objects in Python has its own unique id. The id is assigned to the object when it is created. The id is the object's memory address, and will be different for each time you run the program. ( except for some object that has a constant unique id, like integers from -5 to 256)

Can I use id as variable in Python?

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.

Which is not a keyword in Python?

1 Answer. The correct answer to the question “Which of the following is not a Keyword in Python” is option (a). Val. As Val is not a correct keyword, in Python and all others are keywords.

Is name a keyword in Python?

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language.


2 Answers

You can also get help from python:

>>> help(id) 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.) 

or alternatively you can question IPython

IPython 0.10.2   [on Py 2.6.6] [C:/]|1> id?? Type:           builtin_function_or_method Base Class:     <type 'builtin_function_or_method'> String Form:    <built-in function id> Namespace:      Python builtin Docstring [source file open failed]:     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.) 
like image 37
joaquin Avatar answered Oct 02 '22 12:10

joaquin


id is not a keyword in Python, but it is the name of a built-in function.

The keywords are:

and       del       from      not       while as        elif      global    or        with assert    else      if        pass      yield break     except    import    print class     exec      in        raise continue  finally   is        return def       for       lambda    try 

Keywords are invalid variable names. The following would be a syntax error:

if = 1 

On the other hand, built-in functions like id or type or str can be shadowed:

str = "hello"    # don't do this 
like image 66
Greg Hewgill Avatar answered Oct 02 '22 12:10

Greg Hewgill