简体   繁体   English

从Python中的类中的函数获取返回值

[英]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 我试图通过一个带有返回值的简单函数简单地从我的类中获取值,我确定它是一个微不足道的错误,但我对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. scoreObj.getEnemies是对该方法的引用。 If you want to call it you need parentheses: scoreObj.getEnemies() . 如果你想调用它,你需要括号: scoreObj.getEnemies()

You should think about why you are using a method for this instead of just reading self.num_enemies directly. 您应该考虑为什么使用这种方法而不是直接读取self.num_enemies There is no need for trivial getter/setter methods like this in Python. 在Python中不需要像这样的普通getter / setter方法。

The first parameter for a member function in python is a reference back to the Object. python中成员函数的第一个参数是返回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. 每当我在python中得到关于参数类型的奇怪错误时,我会检查是否忘记了自我参数。 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 . 类函数必须始终采用一个参数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. 其中x是类对象,它将作为根对象传递给getEnemies() ,这意味着发送给类的第一个参数。

Secondly, when calling functions within a class (or at all), always end with () since that's the definition of calling something in Python. 其次,当在一个类(或根本)中调用函数时,总是以()结束,因为这是在Python中调用某些东西的定义。

Then, ask yourself, "Why am I not fetching 'scoreObj.num_lives' just like so instead? Am I saving processing power?" 然后,问问自己, “为什么我不是这样取得'scoreObj.num_lives'呢?我能节省处理能力吗?” 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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