Return value from nested function 'not defined'

Question:

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!

Asked By: Ryan Thomas

||

Answers:

Yes, you are returning the values but not storing them anywhere

wallet, private_key = await get_user_wallet()
Answered By: Gonzalo Odiard

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()
Answered By: MoldOfDestiny
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.