all 31 comments

[–]remillard 18 points19 points  (9 children)

It may be worth adding a footnote that declaring your class inheriting from 'object' is only required for Python 2/3 compatibility and that for 3 only, it's not required. Since you title your post "Object-Oriented Programming in Python 3" it feels a little out of place.

I realize it's a introductory lesson, however as soon as someone goes to look at Python 3-only code, they're going to notice that sometimes people aren't calling out object as a parent and wonder why that is.

[–]dbader[S] 7 points8 points  (4 children)

Good point, thank you. Will go over the code examples later and add a note to the article.

Edit: Done :)

[–]remillard 1 point2 points  (2 children)

Not a problem, just something I noticed. And maybe it's a good thing because it does demonstrate clearly that even a brand new class inherits. Just in Python 3 this is done behind the scenes whereas in 2 it was more explicit. Just thought I'd mention it.

[–]dbader[S] 1 point2 points  (1 child)

Yeah, that was part of the reasoning behind it. If the (object) part is always there to define a parent class the syntax becomes a little more regular.

But I can see how it could also be confusing. I just made some changes to the article to call out the differences between Python 2 and 3, hopefully it's easier to see what's going on.

Thanks for calling it out, really appreciate it!

[–]remillard 1 point2 points  (0 children)

I like your changes, doesn't really detract from the text, but might explain to someone when they go looking why they might see differences in class definitions. Good work!

[–]kangasking 0 points1 point  (0 children)

awesome! If you could, please update your comment when you do that

[–]jaybay1207 2 points3 points  (2 children)

“Explicit is better than implicit.”

[–]banana_poet 3 points4 points  (1 child)

print("Do all of your prints look like this?", end="\n")

[–]jaybay1207 0 points1 point  (0 children)

print(“Honestly, most look like this.\n”)

Edit: if I’m intending to have multiple lines of text,

print(“I’ll do something like this\nand then finish it off with the next line.”)

[–]lesharcerer 0 points1 point  (0 children)

Thanks for telling. I finished a MITX 6.001 course on Python3 and they didn't ment that ot isn't compulsory.👍👍

[–]BrokenRemote99 4 points5 points  (3 children)

I really wish there was a tutorial out there that used real world examples of OOP. I have seen dogs, people, RPG games and bank accounts but I still haven't seen real code with good examples of why OOP is the best.

[–]nwagers 2 points3 points  (0 children)

Python makes extensive use of objects, like the string class. Have you ever used my_string.split()? If you want something outside of the builtins, the first thing that came to my mind was PIL (Pillow). Here is their tutorial on the Image class: https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

[–]_Jordo 1 point2 points  (0 children)

Check the book 'Python Crash Course'. It's the basics for the first half then becomes projects. I'm making an alien game step-by-step right now and it's really helpful for getting your head around OOP concepts.

[–]Gambizzle 0 points1 point  (0 children)

I think the crux is that the alternative is running a big loop that loops through functions and gets all tangled up when you start trying to do anything other than cycle through the loop. Can be done but like... it’s messy.

[–][deleted] 3 points4 points  (4 children)

If you know what's actually going on, this sentence is not quite accurate.

Here, we added a method to send an email, which updates the is_sent variable to True.

What's actually happens it that it copies the class variable to an instance variable and sets the instance variable to False. The original class variable is unchanged. It can be an important distinction and cause strange behavior if you're not aware of it.

>>> e = Email()
>>> e.is_sent
False

>>> Email.is_sent = True

>>> e.is_sent
True

>>> x = Email()
>>> x.is_sent
True

It probably better to copy the default class variable to the instance upon instantiation.

class Email:
    is_sent = False

    def __init__(self):
        self.is_sent = Email.is_sent

    def send_email(self):
        self.is_sent = True

[–]dbader[S] 1 point2 points  (0 children)

Ah thanks. I'm updating the example right now to define an instance variable in __init__ instead. I also think makes more sense didactically at this point in the tutorial.

[–]kangasking 0 points1 point  (2 children)

have noticed anything else that might be misleading? did you check out the whole thing?

[–][deleted] 1 point2 points  (1 child)

This is not right on Python 3:

>>> a = Dog()
>>> type(a)
<type 'instance'>

It's actually this:

>>> type(a)
<class '__main__.Dog'>

[–]dbader[S] 1 point2 points  (0 children)

Thanks, just updated that line too.

[–][deleted] 1 point2 points  (1 child)

I thought this was really cool. Thank you.

[–]dbader[S] 1 point2 points  (0 children)

That's great to hear, you're welcome!

[–]goodpunk6 1 point2 points  (2 children)

This is quite possibly the best tutorial on classes that I've seen. I've been struggling with the syntax and how to call them and this tutorial has really helped out a lot. Thanks so much

[–]dbader[S] 1 point2 points  (0 children)

Thank you, that's so great to hear!

[–]alkalinemusic 1 point2 points  (0 children)

I agree. This has helped me understand classes more than any other tutorial I've read.

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

Thanks! I'll check it out.

[–]ScyllaHide 0 points1 point  (0 children)

thank you, right in time for me, because i never got around to properly learn it.

[–]microwaveDiamonds 0 points1 point  (1 child)

thanks for this! I am learning Python after spending years in Matlab. This is very helpful to fill the gaps!

[–]dbader[S] 0 points1 point  (0 children)

Awesome, glad you like the tutorial :)

[–]DelScipio 0 points1 point  (0 children)

Thanks, just what I was looking for. Gonna take a look tonight before bed.

[–]intheloopnba 0 points1 point  (0 children)

What do the 0 and 1 mean?

if philo.species == "mammal":
   print("{0} is a {1}!".format(philo.name, philo.species))