Search code examples
pythonnested-function

Return value from nested function 'not defined'


I have a nested function that is returning two values. When I try to call those values I get an error saying its not defined.


    async def wallet(self, interaction:discord.Interaction, button:discord.ui.Button):

        ### Blah blah blah ###
        
        @sync_to_async
        def get_user_wallet():

            private_key = abc
            wallet = xyz

            return wallet, private_key

        await get_user_wallet()
        view = secretKeyView(private_key) #private_key is not defined#
        await interaction.response.send_message()

I'm not quite sure what is off, I am pretty sure I am returning the values properly why is it saying it's not defined?

I appreciate any clarity you can give. Thanks!


Solution

  • I am going to assume that instead of abc and xyz you are using actual values. If you are not then they would be your issue. You try to pass the variable private_key to the function secretKeyView(). But you don't actually define private_key outside the scope of your get_user_wallet() function. This function returns values so if you want to use them, you must assign the return value of this function to a variable(s). See below:

    wallet, private_key = await get_user_wallet()
    view = secretKeyView(private_key) #private_key IS defined#
    await interaction.response.send_message()