Storing values in a function to be used later

Asked

Viewed 27 times

0

Hello guys I’m having a problem, I’m wondering if in Python 3.x would have the possibility of a function to store a previously defined value for later use at another time of the code. As shown in the following example.

First follows a class built at a given time

class login(QMainWindow):
def __init__(self, *args, **argvs):
    super(login, self).__init__(*args, **argvs)
    self.ui = Ui_login()
    self.ui.setupUi(self)
    self.ui.button_enter.clicked.connect(self.login)


def login(self):
    user = self.ui.user_input.text()
    username = login_username_db()
    for i in range(0, len(username)):
        if user == username[i][0]:
            passworld = login_pass_db(user)
            senha = self.ui.pass_input.text()
            if senha == passworld[0][0]:
                #emailogin(user)
                load_app(user)

The function load_app(user) has its construction done as follows.

def load_app (user):
    cursor = Database.cursor()
    cursor.execute(f'SELECT app1 FROM `{user}` WHERE ID=1')
    loadapp = cursor.fetchall()

Next is the class that in a future will call the function def load_app(user) only to take the value it will possess (if possible).

class userColaborador(QMainWindow):
      def __init__(self, *args, **argvs):
          super(userColaborador, self).__init__(*args, **argvs)
          self.ui = Ui_userColaborador()
          self.ui.setupUi(self)
          self.ui.btn_exit.clicked.connect(self.exit)
          app = load_app()

   

It is in this question that I do not know how to "save" the data built in function app = load_app() when called upon by the class class login(QMainWindow) so that the class class userColaborador(QMainWindow) may catch you at some point when called.

1 answer

1


If you want this value to be stored, do your load_app() function as a class method (within the class you define def load_app (self, user): and in the end you do self.loadapp = cursor.fetchall(). Ready, the loadapp result will be saved to your object. Another option is to use a class variable (static). Let’s assume that load_app is from the login class. Your code would look like this:

class Login(QMainWindow):
    def __init__(self, *args, **argvs):
        super(login, self).__init__(*args, **argvs)
        self.ui = Ui_login()
        self.ui.setupUi(self)
        self.ui.button_enter.clicked.connect(self.login)


    def login(self):
        user = self.ui.user_input.text()
        username = login_username_db()
        for i in range(0, len(username)):
            if user == username[i][0]:
                passworld = login_pass_db(user)
                senha = self.ui.pass_input.text()
                if senha == passworld[0][0]:
                    #emailogin(user)
                    load_app(user)

    def load_app (user):
        cursor = Database.cursor()
        cursor.execute(f'SELECT app1 FROM `{user}` WHERE ID=1')
        Login.loadapp = cursor.fetchall()

class UserColaborador(QMainWindow):
    def __init__(self, *args, **argvs):
        super(userColaborador, self).__init__(*args, **argvs)
        self.ui = Ui_userColaborador()
        self.ui.setupUi(self)
        self.ui.btn_exit.clicked.connect(self.exit)
        app = Login.loadapp

P.S.: I changed the name of the class of login for Login because it is common for python to use uppercase letter for class name. I did the same with UserColaborador

Browser other questions tagged

You are not signed in. Login or sign up in order to post.