0

Look at the following extremely simple Python code:

x = 3
print(x)

Is assignment in python same as the concept of pointers in C language — like in the above case, x is a pointer to an object of type int — (Correct me if I am wrong)? Does that object only contain the value 3 or it contains some other things as well? Is it completely different from that? Please explain in detail.

Niraj Raut
  • 205
  • 2
  • 7
  • 2
    Read https://nedbatchelder.com/text/names.html – DeepSpace May 03 '20 at 15:32
  • https://stackoverflow.com/questions/20699736/why-should-i-refer-to-names-and-binding-in-python-instead-of-variables-and – Code Pope May 03 '20 at 15:33
  • I'd phrase it as `x` is a reference to an object of type `int`. That's similar to a pointer, but you can't reference the pointer directly (for example you can't step to the 'next' value). In the case of an `int`, the value `3` is the only unique part, but it has access to all of the class methods. So there are parallels, but don't try too hard to match things. Focus more on how Python behaves. – hpaulj May 03 '20 at 15:42
  • I think a closer analogy is a key/value pair in a C++ `map`. Python looks up the name "x" and grabs the paired object. In cpython, the value is a pointer to an address in memory starting with a standard object header and then a specialization for type. Notice that "x" isn't the pointer, its buried in the map. You can't do something like `&x = other_int_ref;`. Since you can't directly touch the pointer, there are fewer places to make mistakes. – tdelaney May 03 '20 at 15:50

0 Answers0