Stupidity costs time

by Jeremy Jones

After rolling out my wife's site, I decided to build two of my own - in Django, of course. I won't get into what they are, but I created a data class (think database table) with a __repr__ attribute (I'll get to what it's for in a second) that looked like this:

def __repr__(self):
return "%s" % (name)

Django's database approach uses a data object's __repr__ attribute to determine how it displays in the admin interface and in forms when you are taking advantage of all the Django-form-goodness-stuff ™. The particular class I defined the above __repr__ on has a "name" attribute, and I wanted the display value to be the object's "name". Clearly, I should have prefixed "name" with "self.". Even though I was able to create a new record for this data type in the Django admin, this apparently kept the data from rendering in the admin interface and all I was getting was an error after creating the record. Now that I have my code fixed, I need to dig in a little bit and see if there is anything that Django could have done to make the error messages a little more helpful. Except for this little bump, due to my own stupidity, everything has been smooth sailing.

3 Comments

Joseph-H
2006-02-21 09:52:54
Python's hacked-on OO bites
Now if you were using a language that didn't require you to put self in front of every single reference to the current object, this wouldn't have happened.
jmjones
2006-02-21 10:08:16
Python's hacked-on OO bites
That's an interesting perspective that I can't totally disagree with. You're correct. If I wouldn't have to specifically refer to the current object, this wouldn't have happened. However, I do disagree that Python's OO is hacked on and that it bites. This type of thing gets really subjective really quickly. I like the explicit reference to the current object and I think it would bother me to have a variable/attribute lookup hierarchy that looked in the current object first, then walked some tree looking for variables/attributes. Python's OO is more of a "pragmatic OO". It's not religiously OO, nor are its OO features "bolted on". It allows you to be OO where it makes sense without forcing you to be OO where it doesn't.
RickCopeland
2006-02-21 13:19:21
Python's hacked-on OO bites
I definitely know that it's not hacked-on (it's been there since the beginning), and I also wouldn't say that it bites. Others have written at length about the use of explicit "self", so I'll link them rather than quoting here:
Python FAQ (http://www.python.org/doc/faq/general.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls)
Several posts at zephyrfalcon.org:
The importance of being selfish (http://zephyrfalcon.org/weblog/arch_d7_2003_07_12.html#e283)
...part 2 (http://zephyrfalcon.org/weblog/arch_d7_2003_07_26.html#e298)
Deja Vu (http://zephyrfalcon.org/weblog2/arch_e10_00770.html#e776)