Declare variable to function without running it












0















def user(choose):
if (choose == "1"):
play = game()
elif (choose == "2"):
return stats(play)
else:
return quit()


I want to take the value from function game() and use it in stats(), but I get an error saying that play is not defined. How do I declare game() and use it in another function?










share|improve this question




















  • 2





    What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

    – Jon Clements
    Nov 25 '18 at 18:09













  • a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

    – Paritosh Singh
    Nov 25 '18 at 18:09






  • 1





    Check if this is not a so called XY Problem . What are you actually trying to do?

    – Paritosh Singh
    Nov 25 '18 at 18:10











  • I was not clear. I've updated the post

    – Gringo
    Nov 25 '18 at 18:18






  • 1





    Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

    – vav
    Nov 25 '18 at 18:36
















0















def user(choose):
if (choose == "1"):
play = game()
elif (choose == "2"):
return stats(play)
else:
return quit()


I want to take the value from function game() and use it in stats(), but I get an error saying that play is not defined. How do I declare game() and use it in another function?










share|improve this question




















  • 2





    What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

    – Jon Clements
    Nov 25 '18 at 18:09













  • a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

    – Paritosh Singh
    Nov 25 '18 at 18:09






  • 1





    Check if this is not a so called XY Problem . What are you actually trying to do?

    – Paritosh Singh
    Nov 25 '18 at 18:10











  • I was not clear. I've updated the post

    – Gringo
    Nov 25 '18 at 18:18






  • 1





    Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

    – vav
    Nov 25 '18 at 18:36














0












0








0








def user(choose):
if (choose == "1"):
play = game()
elif (choose == "2"):
return stats(play)
else:
return quit()


I want to take the value from function game() and use it in stats(), but I get an error saying that play is not defined. How do I declare game() and use it in another function?










share|improve this question
















def user(choose):
if (choose == "1"):
play = game()
elif (choose == "2"):
return stats(play)
else:
return quit()


I want to take the value from function game() and use it in stats(), but I get an error saying that play is not defined. How do I declare game() and use it in another function?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 18:17







Gringo

















asked Nov 25 '18 at 18:05









GringoGringo

137




137








  • 2





    What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

    – Jon Clements
    Nov 25 '18 at 18:09













  • a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

    – Paritosh Singh
    Nov 25 '18 at 18:09






  • 1





    Check if this is not a so called XY Problem . What are you actually trying to do?

    – Paritosh Singh
    Nov 25 '18 at 18:10











  • I was not clear. I've updated the post

    – Gringo
    Nov 25 '18 at 18:18






  • 1





    Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

    – vav
    Nov 25 '18 at 18:36














  • 2





    What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

    – Jon Clements
    Nov 25 '18 at 18:09













  • a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

    – Paritosh Singh
    Nov 25 '18 at 18:09






  • 1





    Check if this is not a so called XY Problem . What are you actually trying to do?

    – Paritosh Singh
    Nov 25 '18 at 18:10











  • I was not clear. I've updated the post

    – Gringo
    Nov 25 '18 at 18:18






  • 1





    Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

    – vav
    Nov 25 '18 at 18:36








2




2





What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

– Jon Clements
Nov 25 '18 at 18:09







What do you mean? If you don't run the function it can't return anything... (or do anything for that matter)

– Jon Clements
Nov 25 '18 at 18:09















a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

– Paritosh Singh
Nov 25 '18 at 18:09





a function "runs". That's its job. you want a function that knows what to return but skips over all the lines before returning a value? That is....strange, and very...unfunctionlike. what is the point of "code" in function1 then? Can it even be done? No, a function will evaluate things and cannot return results until it runs.

– Paritosh Singh
Nov 25 '18 at 18:09




1




1





Check if this is not a so called XY Problem . What are you actually trying to do?

– Paritosh Singh
Nov 25 '18 at 18:10





Check if this is not a so called XY Problem . What are you actually trying to do?

– Paritosh Singh
Nov 25 '18 at 18:10













I was not clear. I've updated the post

– Gringo
Nov 25 '18 at 18:18





I was not clear. I've updated the post

– Gringo
Nov 25 '18 at 18:18




1




1





Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

– vav
Nov 25 '18 at 18:36





Hi Gringo, when you fix your questions, try to keep as much as possible from original, just keep adding "clarifications". Otherwise comments/answers for future readers look really strange.

– vav
Nov 25 '18 at 18:36












1 Answer
1






active

oldest

votes


















1














You could "postpone" execution of func1:



def func1():
return 'abc'

def something_else(callable):
callable()

def main():
hello = None

def f():
"""set result of func1 to variable hello"""
nonlocal hello
hello = func1()

# over here func1 is not executed yet, hello have not got its value
# you could pass function f to some other code and when it is executed,
# it would set result for hello
print(str(hello)) # would print "None"
call_something_else(f)
print(str(hello)) # would print "abc"

main()


After question has changed...



Right now, your local variable play is out of scope for stats.
Also, looks like you expect that function would be called twice.
You need to save play in global content



play = None  # let's set it to some default value
def user(choose):
global play # let python know, that it is not local variable

if choose == "1": # no need for extra brackets
play = game()

if choose == "2" and play: # double check that play is set
return stats(play)

return quit()





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470367%2fdeclare-variable-to-function-without-running-it%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You could "postpone" execution of func1:



    def func1():
    return 'abc'

    def something_else(callable):
    callable()

    def main():
    hello = None

    def f():
    """set result of func1 to variable hello"""
    nonlocal hello
    hello = func1()

    # over here func1 is not executed yet, hello have not got its value
    # you could pass function f to some other code and when it is executed,
    # it would set result for hello
    print(str(hello)) # would print "None"
    call_something_else(f)
    print(str(hello)) # would print "abc"

    main()


    After question has changed...



    Right now, your local variable play is out of scope for stats.
    Also, looks like you expect that function would be called twice.
    You need to save play in global content



    play = None  # let's set it to some default value
    def user(choose):
    global play # let python know, that it is not local variable

    if choose == "1": # no need for extra brackets
    play = game()

    if choose == "2" and play: # double check that play is set
    return stats(play)

    return quit()





    share|improve this answer






























      1














      You could "postpone" execution of func1:



      def func1():
      return 'abc'

      def something_else(callable):
      callable()

      def main():
      hello = None

      def f():
      """set result of func1 to variable hello"""
      nonlocal hello
      hello = func1()

      # over here func1 is not executed yet, hello have not got its value
      # you could pass function f to some other code and when it is executed,
      # it would set result for hello
      print(str(hello)) # would print "None"
      call_something_else(f)
      print(str(hello)) # would print "abc"

      main()


      After question has changed...



      Right now, your local variable play is out of scope for stats.
      Also, looks like you expect that function would be called twice.
      You need to save play in global content



      play = None  # let's set it to some default value
      def user(choose):
      global play # let python know, that it is not local variable

      if choose == "1": # no need for extra brackets
      play = game()

      if choose == "2" and play: # double check that play is set
      return stats(play)

      return quit()





      share|improve this answer




























        1












        1








        1







        You could "postpone" execution of func1:



        def func1():
        return 'abc'

        def something_else(callable):
        callable()

        def main():
        hello = None

        def f():
        """set result of func1 to variable hello"""
        nonlocal hello
        hello = func1()

        # over here func1 is not executed yet, hello have not got its value
        # you could pass function f to some other code and when it is executed,
        # it would set result for hello
        print(str(hello)) # would print "None"
        call_something_else(f)
        print(str(hello)) # would print "abc"

        main()


        After question has changed...



        Right now, your local variable play is out of scope for stats.
        Also, looks like you expect that function would be called twice.
        You need to save play in global content



        play = None  # let's set it to some default value
        def user(choose):
        global play # let python know, that it is not local variable

        if choose == "1": # no need for extra brackets
        play = game()

        if choose == "2" and play: # double check that play is set
        return stats(play)

        return quit()





        share|improve this answer















        You could "postpone" execution of func1:



        def func1():
        return 'abc'

        def something_else(callable):
        callable()

        def main():
        hello = None

        def f():
        """set result of func1 to variable hello"""
        nonlocal hello
        hello = func1()

        # over here func1 is not executed yet, hello have not got its value
        # you could pass function f to some other code and when it is executed,
        # it would set result for hello
        print(str(hello)) # would print "None"
        call_something_else(f)
        print(str(hello)) # would print "abc"

        main()


        After question has changed...



        Right now, your local variable play is out of scope for stats.
        Also, looks like you expect that function would be called twice.
        You need to save play in global content



        play = None  # let's set it to some default value
        def user(choose):
        global play # let python know, that it is not local variable

        if choose == "1": # no need for extra brackets
        play = game()

        if choose == "2" and play: # double check that play is set
        return stats(play)

        return quit()






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 18:29

























        answered Nov 25 '18 at 18:23









        vavvav

        3,39911130




        3,39911130






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470367%2fdeclare-variable-to-function-without-running-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)