0

Coming from C++ and C, how exactly do python variables reference class instances and objects?

I think of them like pointers, but how does python "dereference" these variables?

Ivan17
  • 1
  • 2
    Thinking of them as pointers is fair. But I'm not sure what you're asking. Python dereferences them when it needs to. That's not something you as the programmer need to worry about. – Silvio Mayolo Jul 10 '21 at 17:33
  • I think of python variables as resettable references. That is the assignment resets the reference and does not 'copy' the right hand side to the variable. – quamrana Jul 10 '21 at 17:35
  • Maybe this is beside the point, but class instances and objects are the same thing in Python. – wjandrea Jul 10 '21 at 17:38
  • Class instances *are* objects. Pretty much everything in Python is an object, including numbers, strings, functions, classes, class instances, etc. Variables reference objects. You can think of them as holding pointers to the objects, or indices into an object array. It's up to the implementation. However, each unique object has a unique id, which you can view with `id(obj)`. – Tom Karzes Jul 10 '21 at 17:38
  • @Tom *"each unique object has a unique id"* -- Caveat: only while the object exists – wjandrea Jul 10 '21 at 17:39
  • Nina Zakharenko - Memory Management in Python - The Basics - PyCon 2016 https://www.youtube.com/watch?v=F6u5rhUQ6dU – Epsi95 Jul 10 '21 at 17:39
  • 1
    @wjandrea Right, once an object is freed the id can be reassigned. – Tom Karzes Jul 10 '21 at 17:40
  • 1
    Mandatory link to [Ned Batchelder](https://nedbatchelder.com/text/names.html) – quamrana Jul 10 '21 at 17:40

0 Answers0