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))
python
add a comment |
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))
python
3
bothpop_ ...
functions usex
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
add a comment |
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))
python
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
python
asked Nov 9 at 21:32
user3124200
674
674
3
bothpop_ ...
functions usex
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
add a comment |
3
bothpop_ ...
functions usex
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 21:37
Tomothy32
4878
4878
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
both
pop_ ...
functions usex
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