This question already has an answer here:
“is” operator behaves unexpectedly with integers
10 answers
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 1;
printf("%pn",(void*)&a);
printf("%pn",(void*)&b);
return 0;
}
Returns 0xbfa7b1c8 and 0xbfa7b1cc
However, the equivalent code in python:
a = 1
b = 1
print(id(a))
print(id(b))
returns 31093816L and 31093816L.
From what I understand, python creates only one instance of 1 and both a and b are pointing to it. What are the pros and cons of the two approaches?
Aucun commentaire:
Enregistrer un commentaire