DEV Community

Discussion on: Dead Simple Python: Classes

Collapse
 
paulboot profile image
Paul Boot

Thanks for the excellent Article.

Some small suggestion: in the setter example the actual line of code that sets the Instance Member self.__captain = value is missing ;-)

class Starship:
    def __init__(self):
        # snip
        self.__captain = "Jean-Luc Picard"

    @property
    def captain(self):
        return self.__captain

    @captain.setter
    def captain(self, value):
        print("What do you think this is, " + value + ", the USS Pegasus? Back to work!")

uss_enterprise = Starship()
print(uss_enterprise.captain)
uss_enterprise.captain = "Wesley"
print(uss_enterprise.captain)

Resulting in this output, Jean-Luc Picard refuses to go away....

uss_enterprise = Starship()
uss_enterprise.captain
>>> 'Jean-Luc Picard'
uss_enterprise.captain = "Wesley"
>>> What do you think this is, Wesley, the USS Pegasus? Back to work!
uss_enterprise.captain
>>> 'Jean-Luc Picard'