Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the return value from a function in a class in Python

Tags:

python

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!

like image 902
user1875195 Avatar asked Feb 08 '13 08:02

user1875195


1 Answers

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.

like image 146
BrenBarn Avatar answered Oct 11 '22 13:10

BrenBarn