简体   繁体   中英

Get the return value from a function in a class in 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!

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.

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

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!

getEnemies是一个函数,所以像任何其他函数scoreObj.getEnemies()一样调用它

你犯了一个简单的错误:

scoreObj.getEnemies()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM