Question

In Python is it bad to create an attribute called 'id'?

I know that there's a function called id so I wouldn't create a function or a variable called id, but what about an attribute on an object?

 45  7729  45
1 Jan 1970

Solution

 44

That's ok, and is pretty common. For example, objects mapped to a database record will often have an "id" attribute mapped to the database "id" column value.

Attributes are always "namespaced" so you have to refer to them via self.id or obj.id so there's no conflict with the built-in function.

2010-08-16

Solution

 8

I do this frequently for classes that abstract database tables where there is often a field called id because there is no reasonable chance of a name conflict. Be advised that some synatax highlighters will mark it as a builtin function.

2010-08-16