简体   繁体   中英

Does anyone know in Python how to pass a variable to another function to be used without running the actual function?

I am new to coding so bear with me. I have function A that is passing the variable x to function B. I don't want function B to run when I pass the variable, rather just be able to access variable x when needed. Basically I have kivy text input on my KV file that when the button is clicked it then executes function B. If I were to run function B in function A the user does not get the chance to input text.

PY FILE

def A():
    x = 1
    B(x)

def B(x):
     print(x)
     print(kivy.textinput.text)

KV FILE:

on_press: root.B()

You could use a class.

class MyClass:
    def __init__(self, x):
        self.x = x

    def call_b(self):
        B(self.x)


c = MyClass(1)

on_press: c.call_b()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM