How does Python know the values already stored in its memory? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Understanding Python's “is” operatorWhich Python memory profiler is recommended?How can I safely create a nested directory in Python?How to return multiple values from a function?How do I parse a string to a float or int in Python?Does Python have a ternary conditional operator?Convert bytes to a string?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentDoes Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
Can gravitational waves pass through a black hole?
Why is arima in R one time step off?
Will I be more secure with my own router behind my ISP's router?
Can't solve system of linear equations (that need simplification first)
What to do with someone that cheated their way though university and a PhD program?
When speaking, how do you change your mind mid-sentence?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
Is it accepted to use working hours to read general interest books?
Is it appropriate to mention a relatable company blog post when you're asked about the company?
Show two Lagrangians are equivalent
What is the evidence that custom checks in Northern Ireland are going to result in violence?
Where to find documentation for `whois` command options?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
France's Public Holidays' Puzzle
Variable does not exist: sObjectType (Task.sObjectType)
How to begin with a paragraph in latex
Coin Game with infinite paradox
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
Married in secret, can marital status in passport be changed at a later date?
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
Does Prince Arnaud cause someone holding the Princess to lose?
How long can a nation maintain a technological edge over the rest of the world?
Simulate round-robin tournament draw
What is /etc/mtab in Linux?
How does Python know the values already stored in its memory?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Understanding Python's “is” operatorWhich Python memory profiler is recommended?How can I safely create a nested directory in Python?How to return multiple values from a function?How do I parse a string to a float or int in Python?Does Python have a ternary conditional operator?Convert bytes to a string?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentDoes Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to know how Python knows (if it knows) that a value-type object is already stored in its memory (and also knows where it is).
For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?
>>> a = 1
>>> b = 1
>>> a is b
True
python python-3.x memory
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 3 more comments
I want to know how Python knows (if it knows) that a value-type object is already stored in its memory (and also knows where it is).
For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?
>>> a = 1
>>> b = 1
>>> a is b
True
python python-3.x memory
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Useprint(hex(id(b)))to check memory address forb
– Yusufsn
Apr 19 at 3:01
1
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
2
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
1
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
1
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11
|
show 3 more comments
I want to know how Python knows (if it knows) that a value-type object is already stored in its memory (and also knows where it is).
For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?
>>> a = 1
>>> b = 1
>>> a is b
True
python python-3.x memory
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to know how Python knows (if it knows) that a value-type object is already stored in its memory (and also knows where it is).
For this code, when assigning the value 1 for b, how does it know that the value 1 is already in its memory and stores its reference in b?
>>> a = 1
>>> b = 1
>>> a is b
True
python python-3.x memory
python python-3.x memory
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Apr 19 at 3:48
Mad Physicist
39.1k1682115
39.1k1682115
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Apr 19 at 2:57
Just A LoneJust A Lone
567
567
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Just A Lone is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Useprint(hex(id(b)))to check memory address forb
– Yusufsn
Apr 19 at 3:01
1
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
2
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
1
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
1
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11
|
show 3 more comments
1
Useprint(hex(id(b)))to check memory address forb
– Yusufsn
Apr 19 at 3:01
1
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
2
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
1
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
1
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11
1
1
Use
print(hex(id(b))) to check memory address for b– Yusufsn
Apr 19 at 3:01
Use
print(hex(id(b))) to check memory address for b– Yusufsn
Apr 19 at 3:01
1
1
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
2
2
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
1
1
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
1
1
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11
|
show 3 more comments
3 Answers
3
active
oldest
votes
Python (CPython precisely) uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.
a = 100000
b = 100000
a is b # False
Wait, what? If you check the address of the numbers, you'll find something interesting:
a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512
a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712
It's called integer cache. You can read more about the integer cache here.
Thanks comments from @KlausD and @user2357112 mentioning, direct access on small integers will be using integer cache, while if you do calculations, though they might equals to a number in range [-5, 256], it's not a cached integer. e.g.
pow(3, 47159012670, 47159012671) is 1 # False
pow(3, 47159012670, 47159012671) == 1 # True
“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”
Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
Also10e5is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython,pow(3, 47159012670, 47159012671) == 1, butpow(3, 47159012670, 47159012671) is not 1.)
– user2357112
Apr 19 at 5:37
add a comment |
If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.
add a comment |
Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think you're missing the point of the question.a == bis obviously true. OP is asking whya is bis true.
– Mad Physicist
Apr 19 at 3:03
add a comment |
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
);
);
Just A Lone 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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55756029%2fhow-does-python-know-the-values-already-stored-in-its-memory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Python (CPython precisely) uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.
a = 100000
b = 100000
a is b # False
Wait, what? If you check the address of the numbers, you'll find something interesting:
a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512
a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712
It's called integer cache. You can read more about the integer cache here.
Thanks comments from @KlausD and @user2357112 mentioning, direct access on small integers will be using integer cache, while if you do calculations, though they might equals to a number in range [-5, 256], it's not a cached integer. e.g.
pow(3, 47159012670, 47159012671) is 1 # False
pow(3, 47159012670, 47159012671) == 1 # True
“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”
Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
Also10e5is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython,pow(3, 47159012670, 47159012671) == 1, butpow(3, 47159012670, 47159012671) is not 1.)
– user2357112
Apr 19 at 5:37
add a comment |
Python (CPython precisely) uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.
a = 100000
b = 100000
a is b # False
Wait, what? If you check the address of the numbers, you'll find something interesting:
a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512
a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712
It's called integer cache. You can read more about the integer cache here.
Thanks comments from @KlausD and @user2357112 mentioning, direct access on small integers will be using integer cache, while if you do calculations, though they might equals to a number in range [-5, 256], it's not a cached integer. e.g.
pow(3, 47159012670, 47159012671) is 1 # False
pow(3, 47159012670, 47159012671) == 1 # True
“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”
Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
Also10e5is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython,pow(3, 47159012670, 47159012671) == 1, butpow(3, 47159012670, 47159012671) is not 1.)
– user2357112
Apr 19 at 5:37
add a comment |
Python (CPython precisely) uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.
a = 100000
b = 100000
a is b # False
Wait, what? If you check the address of the numbers, you'll find something interesting:
a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512
a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712
It's called integer cache. You can read more about the integer cache here.
Thanks comments from @KlausD and @user2357112 mentioning, direct access on small integers will be using integer cache, while if you do calculations, though they might equals to a number in range [-5, 256], it's not a cached integer. e.g.
pow(3, 47159012670, 47159012671) is 1 # False
pow(3, 47159012670, 47159012671) == 1 # True
“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”
Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Python (CPython precisely) uses shared small integers to help quick access. Integers range from [-5, 256] already exists in memory, so if you check the address, they are the same. However, for larger integers, it's not true.
a = 100000
b = 100000
a is b # False
Wait, what? If you check the address of the numbers, you'll find something interesting:
a = 1
b = 1
id(a) # 4463034512
id(b) # 4463034512
a = 257
b = 257
id(a) # 4642585200
id(b) # 4642585712
It's called integer cache. You can read more about the integer cache here.
Thanks comments from @KlausD and @user2357112 mentioning, direct access on small integers will be using integer cache, while if you do calculations, though they might equals to a number in range [-5, 256], it's not a cached integer. e.g.
pow(3, 47159012670, 47159012671) is 1 # False
pow(3, 47159012670, 47159012671) == 1 # True
“The current implementation keeps an array of integer objects for all
integers between -5 and 256, when you create an int in that range you
actually just get back a reference to the existing object.”
Why? Because small integers are more frequently used by loops. Using reference to existing objects instead of creating a new object saves an overhead.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Apr 19 at 6:18
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Apr 19 at 3:02
asn-0184asn-0184
2486
2486
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asn-0184 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
Also10e5is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython,pow(3, 47159012670, 47159012671) == 1, butpow(3, 47159012670, 47159012671) is not 1.)
– user2357112
Apr 19 at 5:37
add a comment |
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
Also10e5is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython,pow(3, 47159012670, 47159012671) == 1, butpow(3, 47159012670, 47159012671) is not 1.)
– user2357112
Apr 19 at 5:37
5
5
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
Just to make it clear: this is valid for the CPython interpreter. The language Python does not define this and other interpreters are free to have their own implementation.
– Klaus D.
Apr 19 at 5:07
2
2
Also
10e5 is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython, pow(3, 47159012670, 47159012671) == 1, but pow(3, 47159012670, 47159012671) is not 1.)– user2357112
Apr 19 at 5:37
Also
10e5 is a float, not an int. (Also, not all small ints come from the small int cache. For example, on current CPython, pow(3, 47159012670, 47159012671) == 1, but pow(3, 47159012670, 47159012671) is not 1.)– user2357112
Apr 19 at 5:37
add a comment |
If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.
add a comment |
If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.
add a comment |
If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.
If you take a look at Objects/longobject.c, which implements the int type for CPython, you will see that the numbers between -5 (NSMALLNEGINTS) and 256 (NSMALLPOSINTS - 1) are pre-allocated and cached. This is done to avoid the penalty of allocating multiple unnecessary objects for the most commonly used integers. This works because integers are immutable: you don't need multiple references to represent the same number.
answered Apr 19 at 3:08
Mad PhysicistMad Physicist
39.1k1682115
39.1k1682115
add a comment |
add a comment |
Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think you're missing the point of the question.a == bis obviously true. OP is asking whya is bis true.
– Mad Physicist
Apr 19 at 3:03
add a comment |
Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think you're missing the point of the question.a == bis obviously true. OP is asking whya is bis true.
– Mad Physicist
Apr 19 at 3:03
add a comment |
Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Python doesn't know anything until you tell it. So in your code above, when you initialize a and b, you are storing those values(in the register or RAM), and calling the place to store it a and b, so that you can reference them later. If you didn't initialize the variable first, python would just give you an error.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Apr 19 at 3:02
Monster AR44Monster AR44
111
111
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Monster AR44 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think you're missing the point of the question.a == bis obviously true. OP is asking whya is bis true.
– Mad Physicist
Apr 19 at 3:03
add a comment |
1
I think you're missing the point of the question.a == bis obviously true. OP is asking whya is bis true.
– Mad Physicist
Apr 19 at 3:03
1
1
I think you're missing the point of the question.
a == b is obviously true. OP is asking why a is b is true.– Mad Physicist
Apr 19 at 3:03
I think you're missing the point of the question.
a == b is obviously true. OP is asking why a is b is true.– Mad Physicist
Apr 19 at 3:03
add a comment |
Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.
Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.
Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.
Just A Lone is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f55756029%2fhow-does-python-know-the-values-already-stored-in-its-memory%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
1
Use
print(hex(id(b)))to check memory address forb– Yusufsn
Apr 19 at 3:01
1
>>> hex(id(b))'0x7ffe705ee350' >>> hex(id(a)) '0x7ffe705ee350'
– Just A Lone
Apr 19 at 3:03
2
If two variables refer to the same value between -5 and 256 (as opposed to use) then by definition there is only one object.
– Yusufsn
Apr 19 at 3:04
1
@Yusufsn No. For bigger integers (>256) it's not true.
– asn-0184
Apr 19 at 3:09
1
As I said, only values between -5 and 256
– Yusufsn
Apr 19 at 3:11