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

Help With Phython Classes

Im having trouble with task 3 of this challenge :/

Make a new Warrior method called rage that sets attack_limit to 20.

from character import Character

class Warrior(Character):
  weapon = "sword"
  pass

3 Answers

Ok, well, it is pretty simple:

#define a function
def some_function():
   a_value = 20
   return a_value
class some_class:
   a_value = 20

   #define a class method
   def some_method(self):
      return self.a_value

As you can see, it's very similar to how you would define a function, with maybe one really important difference, which is the "self" argument that the class method HAS to have as its first argument (and sometimes, as in this case, its only argument). The "self" argument allows the method to know about the rest of the contents of the class, similar to the "this" pointer in Java or C++.

NOTE: Calling the first argument "self" is just a convention, you could call it whatever you want.

I'm not quite sure what you are asking. Is it about how you define class methods?

yeah

from character import Character

class Warrior(Character): weapon = 'sword' attack_limit = 20

def rage(self): return Warrior