简体   繁体   English

Python方法不返回任何值

[英]Python method returns none

I have a weird problem. 我有一个奇怪的问题。 I have a function that is supposed to returns list. 我有一个应该返回列表的函数。 I call the function using an object called 'common' and found that it returns None always (common.reset_this). 我使用名为“ common”的对象调用该函数,发现它始终返回None(common.reset_this)。 But when I define this function outside the class, it returns what it is supposed to return. 但是,当我在类之外定义此函数时,它将返回应该返回的内容。 Is there anything I am missing here. 有什么我想念的吗?

class something_else():

    //
    ...

    //
    def reset_this(self,unit = 0):
        with self.__reset_lock:
            status = self.get_status(unit)
        return status

Assuming it's not a typo you're missing the self argument to your method, you need: 假设这不是拼写错误,则说明您缺少方法的self参数,您需要:

class something_else():
    #
    # ...
    #
    def reset_this(self, unit = 0):
        with common.__reset_lock:
            status = common.get_status(unit)
        return status

Without it unit is getting passed the self object (as opposed to 0) because python doesn't care what you call it, the first argument is always the "self" object (the object the method is being called on). 没有它,单元将传递self对象(而不是0),因为python不在乎您如何调用它,第一个参数始终是“ self”对象(调用该方法的对象)。

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

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