Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:代码分析显示OOP中存在多个未定义的错误_Python_Oop_Methods_Spyder - Fatal编程技术网

Python:代码分析显示OOP中存在多个未定义的错误

Python:代码分析显示OOP中存在多个未定义的错误,python,oop,methods,spyder,Python,Oop,Methods,Spyder,我定义了一个名为Email的新类,并且必须创建一些方法。问题是,我在构造函数中定义了变量,Spyder上的代码分析一直说,收件箱,是垃圾邮件和已读在我的一些方法中没有定义,尽管我在构造函数中定义了它 我到处都找遍了,但我真的不知道我做错了什么 我正在使用Spyder 4.1.5和Python 3.8 我的代码如下: class Email(object): def __init__(self, has_been_read, email_contents, is_spam, fr

我定义了一个名为Email的新类,并且必须创建一些方法。问题是,我在构造函数中定义了变量,Spyder上的代码分析一直说,
收件箱
是垃圾邮件
已读
在我的一些方法中没有定义,尽管我在构造函数中定义了它

我到处都找遍了,但我真的不知道我做错了什么

我正在使用Spyder 4.1.5和Python 3.8

我的代码如下:

class Email(object):
    
    def __init__(self, has_been_read, email_contents, is_spam, from_address, inbox):
        assert type(has_been_read) == bool and type(is_spam) == bool
        self.has_been_read = has_been_read
        self.email_contents = email_contents
        self.is_spam = is_spam
        self.from_address = from_address
        self.__inbox = []
        
    def mark_as_read(self):
        # Creating method to change has_been_read boolean to True
        return self.has_been_read == True
    
    def mark_as_spam(self):
        # Creating method to change is_spam to True
        return self.is_spam == True
    
    def add_email(self, eamil_contents, from_address):
        return Email(False, email_contents, False, from_address)
        inbox.append(Email)
        
    def get_count(self):
        print(len.inbox)
        
    def get_email(self, i):
        print(inbox[i].email_contents)
        return inbox[i].mark_as_read
    
    def get_unread_emails(self):
        for i in inbox:
            if has_been_read == False:
                print(i) 
                
    def get_spam_emails(self):
        for i in inbox:
            if is_spam == True:
                print(i)
                
    def delete(self, i):
        del self.inbox[i]

注意每个实例方法的第一个参数是如何
self
。这是对类实例的引用。因此,如果您想通过这些方法之一访问字段
inbox
,则需要参考实例
self
。比如说

def get_count(self):
    print(len(self.inbox))
如果没有,则告诉python解释器查找未定义的局部变量
inbox
,即使
self.inbox
已定义


显然,这与@go2nirvana指出的是一样的。

您忘记了使用
self
访问它们
self.\uu收件箱
self.is\u垃圾邮件
等。一个问题是,您不应该在return语句中使用两个“=”符号,如下所示:
return self.has\u read==True
作为旁注,您确定要使用以双下划线开头的名称作为
\uu收件箱
吗?@go2nirvana谢谢,这很有效!我不知道我怎么会错过that@ThierryLathuille我甚至没有意识到这一点,所以感谢大家的提醒,我去掉了两个下划线