How to get the return value from a function in a class in Python?

PythonServer Side ProgrammingProgramming

The following code shows how to get return value from a function in a Python class

Example

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

    def setScore(self, num):
        self.score = num
    def getScore(self):
         return self.score
    def getEnemies(self):
        return self.num_enemies
    def getLives(self):
        return self.num_lives
       
s = Score()
s.setScore(9)
print s.getScore()
print s.getEnemies()
print s.getLives()

Output

9
5
3
raja
Published on 29-Jan-2018 11:43:42
Advertisements