F
F
Fizl2021-04-01 09:44:48
Python
Fizl, 2021-04-01 09:44:48

How to use a variable inside a class (not an attribute) outside of it?

Hello! I need to specify a variable bodyinside the class, which is not its attribute, outside of it. How to do it?
Here is the class code where this variable is located:

class get_mail_class():
    def __init__(self, mail):
        self.mail = mail 
        self.email_file = codecs.open('email.txt', 'r')
        self.password_file = codecs.open('password.txt', 'r')

        self.read_email_file = str(self.email_file.read())
        self.read_password_file = str(self.password_file.read()) 
        (self.retcode, self.capabilities) = self.mail.login(self.read_email_file, self.read_password_file)
        

        

    def get_mail_func(self):

        my_file = open('text.txt', 'a+')

        self.mail.list()
        self.mail.select('inbox')
        n=0
        (retcode, messages) = self.mail.search(None, '(UNSEEN)')
        if retcode == 'OK':
            for num in messages[0].split():
                n=n+1
                typ, data = self.mail.fetch(num,'(RFC822)')
                for response_part in data:
                    if isinstance(response_part, tuple):
                        original = email.message_from_bytes(response_part[1])
                        raw_email = data[0][1]
                        raw_email_string = raw_email.decode('utf-8')
                        email_message = email.message_from_string(raw_email_string)
                        typ, data = self.mail.store(num,'+FLAGS','\\Seen')

                        if email_message.is_multipart():
                            for payload in email_message.get_payload():
                                
                                body = payload.get_payload(decode=True).decode('utf-8')
                                print(original['From'])
                                print(body)


                                #self.dialog.text.configure(state = 'normal')
                                #self.dialog.text.insert(tk.INSERT, body)
                                #self.dialog.text.configure(state = 'disabled')
                                my_file.write((f"\n {body}"))
                                
                        else:
                            
                            body = email_message.get_payload(decode=True).decode('utf-8')
                            print(original['From'])
                            print(body)

                            my_file.write((f"\n {body}"))

And here is the place where you need to specify this variable:
def main_dialog_thread():
        thread_ins = threading.Thread(target = dialog_class.insert_mail, args = ('''Здесь должна быть переменная body''', ), daemon = True) 
        thread_ins.start()

Thanks in advance!
PS
Do not pay attention to the imap and threading code, everything is fine with it =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2021-04-01
@Fizl

body = email_message.get_payload(decode=True).decode('utf-8')

exists only within the get_mail_func method, will be destroyed upon completion of execution.
What's the problem with storing it as a class field?
class get_mail_class():
    body = None
    .....
    get_mail_class.body = email_message.get_payload(decode=True).decode('utf-8')
....


def main_dialog_thread():
        thread_ins = threading.Thread(target = dialog_class.insert_mail, args = (get_mail_class.body), daemon = True) 
        thread_ins.start()

P.S.
Naming convention

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question