__repr__ should follow python style guide in tutorial

Issue #341 resolved
Former user created an issue

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)

  1. Log in to comment