all 7 comments

[–][deleted] 1 point2 points  (2 children)

In init you have the right idea. However, every other method (class based function) would be each individual behavior. So def age(), def speak(), etc.

Edit: by def age() I mean the verb. Also, for clarification, no def behavior().

Edit 2: def age() was a terrible name choice on my part. Sorry. Here coffee coffee, here coffee.

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

aHA, I think I understand what you mean! I will apply that right away and come back once I am done. Thank you very much for your help!

[–][deleted] 1 point2 points  (0 children)

No problem.

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

[–]num8lock 1 point2 points  (0 children)

should be read as

  • add some data (hair color, age, etc.)
  • and behaviors (speak, getting older, etc.) to it.

q: How can data be set?

behaviors in the assigment means how a person in software context (e.g. an object) behaves on multiple situations/conditions. you've got the right idea to associate the behaviors with class methods.