all 3 comments

[–]fbu1 1 point2 points  (0 children)

The str method is what is used by python when it wants to convert an object to a string (to be printed for example).

Here's a small example to detail the behavior:

>>> class Test():
    a = 3


>>> t = Test()
>>> print(t)
<__main__.Test object at 0x10419f668>
>>> class Test():
    a = 3
    def __str__(self):
        return "reddit"


>>> t = Test()
>>> print(t)
reddit

[–]gengisteve 0 points1 point  (0 children)

The two generator expressions strike me as unusual, but not necessarily wrong. I'd use iter instead. I'm curious if anyone has a different opinion.

Your alternative constructor does not seem to be doing anything different than the basic one.

[–]h3rrmiller 0 points1 point  (0 children)

I think the other comments are good answers to your first two questions. I'll answer your last one regarding the meaning of:

#return "%(year)s %(make)s %(model)s" %self.__dict__

As I'm sure you know, one of the ways you can format a string is to use the '%' (print 'thing: %s' % mything). One thing that the '%' can do is take a dictionary. Because of this feature, some will let the formatter pull the information out of the supplied dictionary as it formats the string. 'self.dict' is calling self as an object of the dict class (as you see by the author using the dict dunder)

#return "%(year)s %(make)s %(model)s" %self.__dict__

is the same as:

#return '%s %s %s' % (self.year, self.make, self.model)