0

I've defined a new class called Email, and had to create a few methods. The problem is, I define the variables in the constructor, and the code analysis on Spyder keeps saying that inbox, is_spam and has_been_read aren't defined in some of my methods, even though I defined it in the constructor.

I have searched everywhere, but I really can't figure out what I am doing wrong?

I am using Spyder 4.1.5 and Python 3.8

My code is as below:

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]
  • 2
    You are forgetting to access them with `self`. `self.__inbox`, `self.is_spam` etc. – go2nirvana Nov 02 '20 at 14:29
  • One problem is that you shouldn't use two "=" signs in the return statements as this: `return self.has_been_read == True` – George Bou Nov 02 '20 at 14:31
  • As a side note, are you sure that you want to use [name mangling](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name) by using a name starting with a double underscore for `__inbox`? – Thierry Lathuille Nov 02 '20 at 14:33
  • @go2nirvana Thanks, that worked! I don't know how I missed that – Jeandre Verster Nov 02 '20 at 15:10
  • @ThierryLathuille I wasn't even aware of that, so thanks for the heads up, I removed the two underscores – Jeandre Verster Nov 02 '20 at 15:11

1 Answers1

0

Note how every instance method's first argument is self. This is a reference to the class instance. So if you want to access the field inbox from one of those methods, you need to refer to the instance, self. for example

def get_count(self):
    print(len(self.inbox))

if you don't, you're telling the python interpreter to find the local variable inbox which is not defined, even though self.inbox is.

obviously, this is the same thing @go2nirvana pointed out.

Ben
  • 4,980
  • 3
  • 43
  • 84