从一个函数获取返回值并在另一个函数上使用

时间:2015-11-19 16:26:11

标签: python sqlite

我正在尝试在python中做一些看似基本的东西,但我认为我会过度思考它。

我试图从password获取create_password()的返回值,并在我放store_password()

<<HERE>>中使用它

我该如何做到这一点?

def create_password(self):
    characters = string.ascii_letters + string.punctuation  + string.digits
    password =  "".join(choice(characters) for x in range(self.pass_char) )
    return (password)

def store_password(self):
    try:
        connection = sqli.connect('pw.db')
    except:
        print("Error occurred!")

    with connection:
        cur = connection.cursor()
        cur.execute('''
                    CREATE TABLE IF NOT EXISTS my_passwords(
                    id INT PRIMARY KEY AUTOINCREMENT,
                    password text
                    '''
        )
        cur.execute('INSERT INTO my_passwords VALUES(?)', <<HERE>>)

谢谢。

5 个答案:

答案 0 :(得分:1)

这基本上是使用你已经构建的函数create_password(self)并假设它们都属于同一个类,你应该能够在第二个函数create_password(self)中调用store_password(self) < / p>

因此,要返回您创建的密码,您只需拨打create_password(self)来代替<<HERE>>

tabac建议的内容也可以使用,但您需要确保之前必须调用方法create_password(self),以便分配self.password变量。

您的代码将成为

cur.execute('INSERT INTO my_passwords VALUES(?)', self.create_password(self))

答案 1 :(得分:0)

我假设这些方法都属于同一个类。如果是这样,您可以将password附加到类实例并在store_password中使用它。请使用self

def create_password(self):
    # code...
    self.password = password

def store_password(self):
    # Access password by doing self.password

答案 2 :(得分:0)

如果你在一个类中定义了两个函数,那么这一行应该可行。

cur.execute('INSERT INTO my_passwords VALUES(?)', self.create_password())

如果没有,您需要从两者中删除self,,然后写

cur.execute('INSERT INTO my_passwords VALUES(?)', create_password())

答案 3 :(得分:0)

也许这个例子对你的情况有帮助吗?

import random
import sqli
import string


def main():
    password = create_password(20)
    store_password(password)


def create_password(length):
    characters = string.ascii_letters + string.punctuation + string.digits
    password = ''.join(random.choice(characters) for _ in range(length))
    return password


def store_password(password):
    try:
        connection = sqli.connect('pw.db')
    except:
        print('Error occurred!')
    else:
        with connection:
            cur = connection.cursor()
            cur.execute('''CREATE TABLE IF NOT EXISTS my_passwords (
                           id INT PRIMARY KEY AUTOINCREMENT,
                           password text)''')
            cur.execute('INSERT INTO my_passwords VALUES (?)', password)

if __name__ == '__main__':
    main()

如果您提供更多代码(特别是您的方法来自的类,尤其是调用create_password方法的类),修复代码会更容易。

答案 4 :(得分:0)

假设两个函数都在同一个类中,在本例 MyPasswordMaker 中调用。

您可以将密码作为参数传递:

def store_password(self, password):
    try:
        connection = sqli.connect('pw.db')
    except:
        print("Error occurred!")

    with connection:
       cur = connection.cursor()
        cur.execute('''
                    CREATE TABLE IF NOT EXISTS my_passwords(
                    id INT PRIMARY KEY AUTOINCREMENT,
                    password text
                    '''
        )
        cur.execute('INSERT INTO my_passwords VALUES(?)', password)


myPasswordMaker = MyPasswordMaker()
password = myPasswordMaker.create_password()
myPasswordMaker.store_password(password)

或者您可以将密码存储在类变量

class MyPasswordMaker:
    def create_password(self):
        characters = string.ascii_letters + string.punctuation  + string.digits
        self.password =  "".join(choice(characters) for x in range(self.pass_char) )

    def store_password(self):
        try:
            connection = sqli.connect('pw.db')
        except:
            print("Error occurred!")

        with connection:
            cur = connection.cursor()
            cur.execute('''
                CREATE TABLE IF NOT EXISTS my_passwords(
                id INT PRIMARY KEY AUTOINCREMENT,
                password text
                '''
            )
        cur.execute('INSERT INTO my_passwords VALUES(?)', self.password)

或者在 store_password()中调用 create_password()

cur.execute('INSERT INTO my_passwords VALUES (?)', self.create_password())