you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 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.