7

I am trying to simply get the value out of my class using a simple function with a return value, I'm sure its a trivial error, but im pretty new to python

I have a simply class set up like this:

class score():
#initialize the score info 
    def __init__(self):
        self.score = 0
        self.num_enemies = 5
        self.num_lives = 3

    # Score Info
    def setScore(num):
        self.score = num

    # Enemy Info
    def getEnemies():
        return self.num_enemies

    # Lives Info
    def getLives():
        return self.getLives

etc.....

Than I create an instance of the class as such:

scoreObj = score()

for enemies in range(0, scoreObj.getEnemies):
    enemy_sprite.add(enemy())  

I get the error saying that an integer is expected, but it got an instancemethod

What is the correct way to get this information?

Thanks!

2
  • you've forgotten to give "self" to the class methods. Commented Feb 8, 2013 at 8:11
  • 1
    Please don't define trivial getxxx setxxx methods. Python is not Java. Commented Feb 8, 2013 at 8:48

5 Answers 5

6

scoreObj.getEnemies is a reference to the method. If you want to call it you need parentheses: scoreObj.getEnemies().

You should think about why you are using a method for this instead of just reading self.num_enemies directly. There is no need for trivial getter/setter methods like this in Python.

Sign up to request clarification or add additional context in comments.

Comments

4

The first parameter for a member function in python is a reference back to the Object.

Traditionally you call it "self", but no matter what you call the first parameter, it refers back to the "self" object:

Anytime I get weird errors about the type of a parameter in python, I check to see if I forgot the self param. Been bit by this bug a few times.

class score():
#initialize the score info 
    def __init__(self):
        self.score = 0
        self.num_enemies = 5
        self.num_lives = 3

    # Score Info
    def setScore(self, num):
        self.score = num

    # Enemy Info
    def getEnemies(self):
        return self.num_enemies

    # Lives Info
    def getLives(foo): #foo is still the same object as self!!
        return foo.num_lives
        #Works but don't do this because it is confusing

2 Comments

YW. I think forgetting self is the python equivalent of missing curly braces around a multi-line if statement in C. Its easy to do, syntactically correct, and can be hard to spot by just looking at the code.
Makes me wonder why self is not the default setting for python classes, and that if you don't want self then in that case be explicit.
4

This code works:

class score():
    def __init__(self):
        self.score = 0
        self.num_enemies = 5
        self.num_lives = 3

    def setScore(self, num):
        self.score = num

    def getEnemies(self):
        return self.num_enemies

    def getLives(self):
        return self.getLives

scoreObj = score()

for enemy_num in range(0, scoreObj.getEnemies()):
    print enemy_num
    # I don't know what enemy_sprite is, but
    # I commented it out and just print the enemy_num result.
    # enemy_sprite.add(enemy())

Lesson Learned:

Class functions must always take one parameter, self. That's because when you call a function within the class, you always call it with the class name as the calling object, such as:

scoreObj = score()
scoreObj.getEnemies()

Where x is the class object, which will be passed to getEnemies() as the root object, meaning the first parameter sent to the class.

Secondly, when calling functions within a class (or at all), always end with () since that's the definition of calling something in Python.

Then, ask yourself, "Why am I not fetching 'scoreObj.num_lives' just like so instead? Am I saving processing power?" Do as you choose, but it would go faster if you get the values directly from the class object, unless you want to calculate stuff at the same time. Then your logic makes perfect sense!

Comments

2

You made a simple mistake:

scoreObj.getEnemies()

1 Comment

You made a more simple mistake. Methods in classes should have self as a first argument, or cls if using @classmethod decorator and you didn't mention it
1

getEnemies is a function, so call it like any other function scoreObj.getEnemies()

2 Comments

This is what I initially thought it was, but I get the error: TypeError: getEnemies() takes no arguments (1 given)
Missed that. In your function definition you need to pass self in: def getEnemies(self):. Same for any others that need to access self.<something>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.