The stupidity of 'id' as a variable name (or stupidity of me)

16 September 2008   3 comments   Python

Mind That Age!

This blog post is 10 years old! Most likely, its content is outdated. Especially if it's technical.

Both in Zope2 and in Django you need to work with attributes called id. This is a shame since it's such a huge pitfall. Despite having done Python programming for so many years I today fell into this pitfall twice!! The pitfall is that id is a builtin function, not a suitable variable name. The reason is that I was changing a complex app to use something called the UUID as the indentifier instead of the ID which happened to be a name of a primary key in a table.

This meant lots of changes and I tested and tested and kept getting really strange errors. I took the whole thing apart and put it back together when I discovered my error of checking if variable id was set or not. id, if undefined, defaults to the builtin function id() which will always return true on bool(id).

It's been a long day. I'm going home. Two newbie mistakes in one programming session. I'm sure I'm not the only one who's been trapped by this.

Comments

Roberto
I've done that a lot too, now I just use id_ as the variable name.
Peter Bengtsson
I've changed most places to 'oid' as in "object ID".
nih
this is also common in wxPython, i only realised this was an issue after running one of my programs through pylint, which flags up something like 'using builtin function id', so i'm now also using id_

Your email will never ever be published


Related posts

Previous:
Why we like Obama, short answer: his speeches 11 September 2008
Next:
The importance of env (and how it works with virtualenv) 18 September 2008
Related by Keyword:
Sequences in PostgreSQL and rolling back transactions 12 May 2009
Note to self about Jeditable 22 November 2007
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007
Ever wondered how much $87 Billion is? 04 November 2003