简体   繁体   English

Python函数定义不起作用

[英]Python function definition not working

I've got a problem with a function which is defined in a Python class: 我在Python类中定义的函数遇到问题:

class DatabaseHandler:

    def get_messages_by_last_mid(self, uidReceiver, last_mid):

        self.cursor.execute("SELECT uidSender, content FROM messages WHERE MID > ?", str(last_mid))

        ret_value = []
        result = self.cursor.fetchone()
        while result != None:
            ret_value.append(result)
            result = self.cursor.fetchone()

        return ret_value

    def get_messages_by_last_group_id(self, uidReceiver, last_gid):

        self.cursor.execute("SELECT gidreceiver, uidsender, content FROM groupmessages WHERE mid > ?", str(last_gid))

        ret_value = []
        result = self.cursor.fetchone()
        while result != None:
           ret_value.append(result)
           result = self.cursor.fetchone()

        return ret_value

But only the function get_messages_by_last_mid() works, the other one produces the following error: 但是只有函数get_messages_by_last_mid()起作用,另一个函数产生以下错误:

AttributeError: DatabaseHandler instance has no attribute 'get_messages_by_last_group_id'

Thanks in advance :) 提前致谢 :)

Indentation can be a silent killer in Python if you're coming from various other programming languages. 如果您来自其他各种编程语言,则缩进可以成为Python中的沉默杀手。 As you already know, indentation is how Python determines the scope of methods, functions, classes, loops, etc. as you write your code. 如您所知,缩进是Python在编写代码时如何确定方法,函数,类,循环等的范围。 Make sure your indentation is consistent! 确保您的缩进是一致的! You can use the command-line option -t or -tt to python to check yourself. 您可以使用命令行选项-t或-tt对python进行检查。

Sorry guys, I had used an old package. 抱歉,我用的是旧包装。 My problem has been fixed. 我的问题已解决。 Thanks for your answers. 感谢您的回答。

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

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