- changed status to resolved
- changed title to __repr__ should follow python style guide in tutorial
__repr__ should follow python style guide in tutorial
Issue #341
resolved
In the tutorial, the User class is defined like that:
class User(object):
def __repr__(self):
return "(User %s, password:%s)" % (self.user_name, self.password)
I think this is bad style that will be copied by newbies reading the tutorial.
The return value of __repr__
should "look like a valid Python expression that could be used to recreate an object with the same value." Also, repr should be ascii only which is not guaranteed when you return the user_name.
So instead of __repr__
, you should use __str__
here. Or do something like:
class User(object):
def __init__(self, user_name, password):
self.user_name = user_name
self.password = password
def __str__(self):
return self.user_name
def __repr__(self):
return "%s(%r,%r)" % (
self.__class__.__name__, self.user_name, self.password)
Comments (1)
-
repo owner - Log in to comment
normally i am not so into the formal definition of
__repr__
since in many cases its unworkable (such as doing a repr of a Mapper or Table sends out an unreadable and useless mess of hundreds of constructors), but since its the tutorial it probably should be this way, so its in changeset:2007