Using a variable before it’s been defined









up vote
2
down vote

favorite












Why does the code below work when pop_zero and pop_end use the list variable, x, before x has even been defined (x = list(range(i))). In other words pop_zero and pop_end are defined in terms of x, but x is defined after pop_zero and pop_end. Why isn’t this a problem?



import timeit
from timeit import Timer
pop_zero = Timer("x.pop(0)", "from __main__ import x")
pop_end = Timer("x.pop()", "from __main__ import x")
print("pop(0) pop()")
for i in range(1000000,100000001,1000000):
x = list(range(i))
pt = pop_end.timeit(number=1000)
x = list(range(i))
pz = pop_zero.timeit(number=1000)
print("%15.5f, %15.5f" %(pz,pt))









share|improve this question

















  • 3




    both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
    – JacobIRR
    Nov 9 at 21:33











  • @JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
    – roganjosh
    Nov 9 at 21:37














up vote
2
down vote

favorite












Why does the code below work when pop_zero and pop_end use the list variable, x, before x has even been defined (x = list(range(i))). In other words pop_zero and pop_end are defined in terms of x, but x is defined after pop_zero and pop_end. Why isn’t this a problem?



import timeit
from timeit import Timer
pop_zero = Timer("x.pop(0)", "from __main__ import x")
pop_end = Timer("x.pop()", "from __main__ import x")
print("pop(0) pop()")
for i in range(1000000,100000001,1000000):
x = list(range(i))
pt = pop_end.timeit(number=1000)
x = list(range(i))
pz = pop_zero.timeit(number=1000)
print("%15.5f, %15.5f" %(pz,pt))









share|improve this question

















  • 3




    both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
    – JacobIRR
    Nov 9 at 21:33











  • @JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
    – roganjosh
    Nov 9 at 21:37












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Why does the code below work when pop_zero and pop_end use the list variable, x, before x has even been defined (x = list(range(i))). In other words pop_zero and pop_end are defined in terms of x, but x is defined after pop_zero and pop_end. Why isn’t this a problem?



import timeit
from timeit import Timer
pop_zero = Timer("x.pop(0)", "from __main__ import x")
pop_end = Timer("x.pop()", "from __main__ import x")
print("pop(0) pop()")
for i in range(1000000,100000001,1000000):
x = list(range(i))
pt = pop_end.timeit(number=1000)
x = list(range(i))
pz = pop_zero.timeit(number=1000)
print("%15.5f, %15.5f" %(pz,pt))









share|improve this question













Why does the code below work when pop_zero and pop_end use the list variable, x, before x has even been defined (x = list(range(i))). In other words pop_zero and pop_end are defined in terms of x, but x is defined after pop_zero and pop_end. Why isn’t this a problem?



import timeit
from timeit import Timer
pop_zero = Timer("x.pop(0)", "from __main__ import x")
pop_end = Timer("x.pop()", "from __main__ import x")
print("pop(0) pop()")
for i in range(1000000,100000001,1000000):
x = list(range(i))
pt = pop_end.timeit(number=1000)
x = list(range(i))
pz = pop_zero.timeit(number=1000)
print("%15.5f, %15.5f" %(pz,pt))






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:32









user3124200

674




674







  • 3




    both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
    – JacobIRR
    Nov 9 at 21:33











  • @JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
    – roganjosh
    Nov 9 at 21:37












  • 3




    both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
    – JacobIRR
    Nov 9 at 21:33











  • @JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
    – roganjosh
    Nov 9 at 21:37







3




3




both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
– JacobIRR
Nov 9 at 21:33





both pop_ ...functions use x as a string and the string doesn't get evaluated until you invoke the function
– JacobIRR
Nov 9 at 21:33













@JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
– roganjosh
Nov 9 at 21:37




@JacobIRR either it's a dupe or you should flesh out an answer IMO because I'm confused on this
– roganjosh
Nov 9 at 21:37












1 Answer
1






active

oldest

votes

















up vote
1
down vote













When you create a Timer object, it doesn't actually execute the code inside yet. Only when you call one of its method (i.e. .timeit()) does it actually run the code.






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',
    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%2f53233523%2fusing-a-variable-before-it-s-been-defined%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








    up vote
    1
    down vote













    When you create a Timer object, it doesn't actually execute the code inside yet. Only when you call one of its method (i.e. .timeit()) does it actually run the code.






    share|improve this answer
























      up vote
      1
      down vote













      When you create a Timer object, it doesn't actually execute the code inside yet. Only when you call one of its method (i.e. .timeit()) does it actually run the code.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        When you create a Timer object, it doesn't actually execute the code inside yet. Only when you call one of its method (i.e. .timeit()) does it actually run the code.






        share|improve this answer












        When you create a Timer object, it doesn't actually execute the code inside yet. Only when you call one of its method (i.e. .timeit()) does it actually run the code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 21:37









        Tomothy32

        4878




        4878



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233523%2fusing-a-variable-before-it-s-been-defined%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo