Why does semicolon in python make a different result
Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
I found a strange question about semicolon ";" in python.
>>> x=20000;y=20000
>>> x is y
True
>>> x=20000
>>> y=20000
>>> x is y
False
>>> x=20000;
>>> y=20000
>>> x is y
False
Why is it that the first test returns True
and the other two return False
?
Python Version: 3.6.5
python
New contributor
add a comment |Â
up vote
12
down vote
favorite
I found a strange question about semicolon ";" in python.
>>> x=20000;y=20000
>>> x is y
True
>>> x=20000
>>> y=20000
>>> x is y
False
>>> x=20000;
>>> y=20000
>>> x is y
False
Why is it that the first test returns True
and the other two return False
?
Python Version: 3.6.5
python
New contributor
add a comment |Â
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I found a strange question about semicolon ";" in python.
>>> x=20000;y=20000
>>> x is y
True
>>> x=20000
>>> y=20000
>>> x is y
False
>>> x=20000;
>>> y=20000
>>> x is y
False
Why is it that the first test returns True
and the other two return False
?
Python Version: 3.6.5
python
New contributor
I found a strange question about semicolon ";" in python.
>>> x=20000;y=20000
>>> x is y
True
>>> x=20000
>>> y=20000
>>> x is y
False
>>> x=20000;
>>> y=20000
>>> x is y
False
Why is it that the first test returns True
and the other two return False
?
Python Version: 3.6.5
python
python
New contributor
New contributor
edited 38 mins ago
NaruS
6812
6812
New contributor
asked 1 hour ago
longshuai ma
614
614
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
15
down vote
In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int
value in each assignment, and so can (it doesn't have to, but does) make x
and y
references to the same object.
The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ;
that joins two statements into one.
In the following two examples, by the time y=20000
is read and evaluated, x=20000
(with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int
values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y
.
Thanks, i understand. then i also tryx,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
add a comment |Â
up vote
1
down vote
The is
operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True
and sometimes `False+ just to be a matter of luck.
For example, the results are different in an interactive session and in a standalone program:
$ cat test.py
x = 200000; y = 200000
print(x is y)
xx = 200000
yy = 200000
print(xx is yy)
$ python test.py
True
True
Or you have this other example:
>>> x = 50 + 50; y = 50 + 50
>>> x is y
True
>>> x = 5000 + 5000; y = 5000 + 5000
>>> x is y
False
This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000
object. It has nothing to do with the semicolon.
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make theis
operator returnFalse
.
â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int
value in each assignment, and so can (it doesn't have to, but does) make x
and y
references to the same object.
The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ;
that joins two statements into one.
In the following two examples, by the time y=20000
is read and evaluated, x=20000
(with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int
values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y
.
Thanks, i understand. then i also tryx,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
add a comment |Â
up vote
15
down vote
In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int
value in each assignment, and so can (it doesn't have to, but does) make x
and y
references to the same object.
The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ;
that joins two statements into one.
In the following two examples, by the time y=20000
is read and evaluated, x=20000
(with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int
values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y
.
Thanks, i understand. then i also tryx,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
add a comment |Â
up vote
15
down vote
up vote
15
down vote
In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int
value in each assignment, and so can (it doesn't have to, but does) make x
and y
references to the same object.
The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ;
that joins two statements into one.
In the following two examples, by the time y=20000
is read and evaluated, x=20000
(with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int
values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y
.
In the interactive interpreter, the first semi-colon line is read and evaluated in one pass. As such, the interpreter recognizes that 20000 is the same immutable int
value in each assignment, and so can (it doesn't have to, but does) make x
and y
references to the same object.
The important point is that this is simply an optimization that the interactive interpreter chooses to make; it's not something guaranteed by the language or some special property of the ;
that joins two statements into one.
In the following two examples, by the time y=20000
is read and evaluated, x=20000
(with or without the semi-colon) has already ben evaluated and forgotten. Since 20000 isn't in the range (-5 to 257) of pre-allocated int
values, CPython doesn't try to find another instance of 20000 already in memory; it just creates a new one for y
.
answered 1 hour ago
chepner
229k27214311
229k27214311
Thanks, i understand. then i also tryx,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
add a comment |Â
Thanks, i understand. then i also tryx,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".
â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
Thanks, i understand. then i also try
x,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".â longshuai ma
43 mins ago
Thanks, i understand. then i also try
x,y=20000,20000
, the "x is y" returns true also. so i think "x=20000;y=20000" may optimiz to "x,y=20000,20000".â longshuai ma
43 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
Not directly, but again, because the commands are in one "batch" of input, the interpreter/compiler has more opportunity to look for optimizations. The tuple created on the right-hand side consists of the same literal repeated twice, so the tuple can be created with two references to the same object, rather than allocating two separate objects with the same value.
â chepner
23 mins ago
add a comment |Â
up vote
1
down vote
The is
operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True
and sometimes `False+ just to be a matter of luck.
For example, the results are different in an interactive session and in a standalone program:
$ cat test.py
x = 200000; y = 200000
print(x is y)
xx = 200000
yy = 200000
print(xx is yy)
$ python test.py
True
True
Or you have this other example:
>>> x = 50 + 50; y = 50 + 50
>>> x is y
True
>>> x = 5000 + 5000; y = 5000 + 5000
>>> x is y
False
This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000
object. It has nothing to do with the semicolon.
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make theis
operator returnFalse
.
â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
add a comment |Â
up vote
1
down vote
The is
operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True
and sometimes `False+ just to be a matter of luck.
For example, the results are different in an interactive session and in a standalone program:
$ cat test.py
x = 200000; y = 200000
print(x is y)
xx = 200000
yy = 200000
print(xx is yy)
$ python test.py
True
True
Or you have this other example:
>>> x = 50 + 50; y = 50 + 50
>>> x is y
True
>>> x = 5000 + 5000; y = 5000 + 5000
>>> x is y
False
This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000
object. It has nothing to do with the semicolon.
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make theis
operator returnFalse
.
â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The is
operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True
and sometimes `False+ just to be a matter of luck.
For example, the results are different in an interactive session and in a standalone program:
$ cat test.py
x = 200000; y = 200000
print(x is y)
xx = 200000
yy = 200000
print(xx is yy)
$ python test.py
True
True
Or you have this other example:
>>> x = 50 + 50; y = 50 + 50
>>> x is y
True
>>> x = 5000 + 5000; y = 5000 + 5000
>>> x is y
False
This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000
object. It has nothing to do with the semicolon.
The is
operator checks whether two values are the same object in memory. It's not meant to be used for checking for equality. For what is worth, you could consider the fact that it sometimes returns True
and sometimes `False+ just to be a matter of luck.
For example, the results are different in an interactive session and in a standalone program:
$ cat test.py
x = 200000; y = 200000
print(x is y)
xx = 200000
yy = 200000
print(xx is yy)
$ python test.py
True
True
Or you have this other example:
>>> x = 50 + 50; y = 50 + 50
>>> x is y
True
>>> x = 5000 + 5000; y = 5000 + 5000
>>> x is y
False
This happens because the interpreter caches small numbers so they are always the same object, but it doesn't for large numbers, so both additions in the second case create a new 10000
object. It has nothing to do with the semicolon.
edited 31 mins ago
answered 59 mins ago
Roberto Bonvallet
19.9k53349
19.9k53349
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make theis
operator returnFalse
.
â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
add a comment |Â
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make theis
operator returnFalse
.
â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
How come can one consider it to be a matter of luck while it isn't at all?
â Ià Âñk Kaplan
47 mins ago
and when it will return False?
â mad_
43 mins ago
and when it will return False?
â mad_
43 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@Ià ÂñkKaplan You're right that it isn't. What I mean is that it's not a behavior you can rely on. In some sense it's a matter of luck that the interpreter chooses to the optimization that Chepner mentioned, and also that the example was tested in the interactive console (I tried writing a standalone program and the results are different).
â Roberto Bonvallet
34 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the
is
operator return False
.â Roberto Bonvallet
25 mins ago
@mad_ I expanded my answer with an example where two expressions with equal result separated by semicolons make the
is
operator return False
.â Roberto Bonvallet
25 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
There isn't an inconsistency though, you may want to stay away from the immutable built-ins to don't think about interpreter optimizations that much but, for custom classes it is always accurate and something that can be relied upon. If not; I'd like to learn where it can return incorrect result in a custom class.
â Ià Âñk Kaplan
22 mins ago
add a comment |Â
longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.
longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.
longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.
longshuai ma is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52319436%2fwhy-does-semicolon-in-python-make-a-different-result%23new-answer', 'question_page');
);
Post as a guest
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
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
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