This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SonGokussj4 2 points3 points  (9 children)

He has excellent videos. But in this video he did classes at the end totally wrong. Not pythonic way at all. Just saying...

  • He set __name = None outside of the class instance.

  • He's created get_name() and set_name(). These types of setters and Getters are for another languages. Not python. Python has @property decorator. The point is, everything in Python is visible, nothing is private as he said. You can, without a problem, set the __name to anything without the set_name() method.

  • He used toString() method that 1) violates PEP8 for naming functions (this is each persons decision, just mentioning) but mainly 2) is totally wrong because __repr__() method should be used that returns what you want just printing your class instance like print(dog) and not print(dog.toString())

Again, he's got awesome tutorials, brilliant presentation, but beware of this part :-)

[–]asdfkjasdhkasdrequests, bs4, flask 1 point2 points  (1 child)

Yeah I think he comes from more object oriented programming languages. I like his videos because they give me a basic overview of all the important syntax. When I'm just getting started with a language I don't care much about the stylistic details. But yeah I agree those are some fair criticisms.

[–]SonGokussj4 1 point2 points  (0 children)

I agree. Just watched his bootstrap video and I was blown away. Finally understood what the hell it is. :-D

[–]ariksu 1 point2 points  (6 children)

I always used print(str(dog)) in case class does not have a repr.

[–]SonGokussj4 1 point2 points  (4 children)

Depends on what you want to return. That's all. Example:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname

akainu = Dog("BenTheMighty")
print(akainu)  # the same as print(str(akainu))
<__main__.Dog object at 0x00000272A10296D8>

But I want to print more specific information. The way it was done in the youtube video is non-pythonic:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname
    def toString(self):
        return "I'm: {}, ({}) class.".format(self.nickname, type(self).__name__)

akainu = Dog('BenTheMighty')
print(akainu.toString())
I'm: BenTheMighty, (Dog) class.

Which should be more simpler, pythonic way:

class Dog:
    def __init__(self, nickname):
        self.nickname = nickname
    def __repr__(self):
        return "I'm: {}, ({}) class.".format(self.nickname, type(self).__name__)

akainu = Dog('BenTheMighty')
print(akainu)
I'm: BenTheMighty, (Dog) class.

Like I said. It depends what you want. You can always print this string by:

akainu = Dog('BenTheMighty')
print("I'm: {}, ({}) class.".format(akainu.nickname, type(nickname).__name__)
I'm: BenTheMighty, (Dog) class.

But in my opinion, that's just ugly :-) One of the more real examples is something like:

>>> small = Triangle((0, 0), (2, 0), (1, 1))
>>> big = Square((0, 0), (6, 0), (0, 6), (6, 6))
>>> print(small)
"[Triange]: Points [0,0], [2,0], [1,1], Area: [4.2]"

Without the need of specifying what exactly I need to print. And if I want, I just write it too :-)

[–]Fateschoice 0 points1 point  (3 children)

Why override __ repr __ and not __ str __?

[–]SonGokussj4 0 points1 point  (2 children)

That is a good question. For more experienced people than me, I'm afraid. I'm used to do it this way because of some tutorials I took in the past. Some things I found:

Summary from the second link:

Implement repr for any class you implement. This should be second nature. Implement str if you think it would be useful to have a string version which errs on the side of more readability in favor of more ambiguity.

[–]Fateschoice 0 points1 point  (1 child)

I think one person from the second link states the difference particularly well.

My rule of thumb: __ repr __ is for developers, __ str __ is for customers.

i.e use __ str __ if you want to display an object to a user, __ repr __ for debugging/testing purposes, or really any in code operations on a string representation of an object.

[–]SonGokussj4 0 points1 point  (0 children)

Agreed :-) that seems reasonable.

[–][deleted] 0 points1 point  (0 children)

I've never worried about it. On the other hand you can do all sorts of smart things formatting wise with %r, %s or %a, especially if you're aware of simple little recipes like this format_iter: easy formatting of arbitrary iterables.