Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Objects Class Methods

mohammed abdi abdulahi warsame
mohammed abdi abdulahi warsame
4,926 Points

whay are we using def battelcary

i didnt understand the poin of usin def battelcry() and returning it!!!!!! what's the point of doing that!!!

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Mohammed.

Basically defining a method for the class is useful as for example (when the project grows) and you will need to use this in a few places all you will have to do is to call the method like in the video jubjub.battlecry() You could call the uppercasing every single time you want to have the battlecry but this would not be ideal.

Also, this example in the video is a very simple one, just to give you an idea, so it may not seem very useful... But imagine having a method of 5/6 lines of code and you need to use it in several place: With defining the method you will only need to call it on the right instance and you are all set instead of typing the same 5/6 lines of code every single time.

I hope this helped. Vitto

mohammed abdi abdulahi warsame
mohammed abdi abdulahi warsame
4,926 Points

Thanks alot for helping, i was struggling alittle bit to understand it! I have another qustion, i learned it but didnt understand it fully! => def moe(abdi): what can i do with the argument (abdi) and what is the point of setting it there? i usully leave it open and it works!!! Thanks.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

No worries it takes time (a lot) to understand and learn all of these things.

Again, for the new question the concept is similar. We define functions to have pieces of code that we can then easily reuse in different places right? Passing in arguments will make this process possible: we can have previously assigned variables or user inputs or something else that we want to pass in so we need to tell python about this and we do it this way when we define the function.

Let's have a look at a simple example. So this is what you would normally do you said right?

def say_hello_to_mohammed():
    print("Hello Mohammed")

But let's say you want to say hello to different people:

def say_hello(persons_name):
    print("Hello {}".format(persons_name))

I hope it is a little bit clearer already, if you want to read more, follow this link (or just google it, there are plenty of good python resources out there): https://www.tutorialspoint.com/python/python_functions.htm

Vitto

mohammed abdi abdulahi warsame
mohammed abdi abdulahi warsame
4,926 Points

Thanks alot for the help and the effort i appreciate it, it really worked for me