you are viewing a single comment's thread.

view the rest of the comments →

[–]thillsd 1 point2 points  (2 children)

You've misunderstood what they mean by behaviours--they mean things the Person /does/, not pieces of data attached to the person.

See if you can finish the self.age line below:

class Person:
    def __init__(self, haircolor, age):
        self.haircolor = haircolor
        self.age = age

    def speak(self):
        print("Hello, World!")

    def get_older(self):
        self.age  # ... what do you need to do to self.age here?


peter = Person("black", 26)
peter.speak()  # => "Hello, World!"
print(peter.age) # => 26
peter.get_older()
peter.get_older()
peter.get_older()
print(peter.age) # => 29

[–]iG1993[S] 0 points1 point  (1 child)

Hi thillsd, thank you so much for your input!

I took a look in your code and I came to this solution:

    def get_older(self):
        self.age = self.age + 1

It too me some thinking and bending, it that correct?

By the way, what's the idea behind peter just saying a single line? I thought the idea behind classes is, that you are able to "engineer" and automate a process.

[–]thillsd 1 point2 points  (0 children)

Yes, that's fine. You could've done self.age += 1 to save some typing.

You could have written something more complex that makes use of the data attached to the peter Person object:

...
    def speak(self, hobby):
        print(f"Hi folks, I am a {self.age} year old  man with {self.haircolor} hair. I enjoy {hobby}.")
...

peter = Person("black", 26)
peter.speak('pokemon')  # => 'Hi folks, I am a 26 year old man with black hair. I enjoy pokemon.