0

I'm very confused with python variables. I read several chapters from different books on variables and some explanations are contradictory some just differ slightly but still they differ. Some use visual explanations which are not necessarily helpful. The following is my understanding which is basically the intersections of all the chapters facts. I'm looking for someone to correct me so I can put this to rest. I can't sleep. Help me!

Understanding:

Everything in python is an object. Every name of a variable(identifier) is a reference to an object.

I think for a reference you need at least two memory units like this :

<address><content>

The identifier of a variable is connected to the address of the first memory unit. The content of this unit is a connection to another memory unit's address whose content can now be a value (simple data types) or another even another reference (complex data types).

So as of now I think this is how a reference works in general and in python.

Contradictory to that I often see for simple data types that it's displayed in a way that the content of the first unit is already the value itself. This aligns with my understanding of a primitive data type in C.

So can somebody please bring order into my mental chaos?

Thanks everyone for taking the time! I really appreciate it.

juanpa.arrivillaga
97.7k14 gold badges141 silver badges190 bronze badges
asked Aug 26, 2020 at 17:12

1 Answer 1

1

In CPython, variables access their value by following a C pointer of type PyObject*. The pointer points to a struct which may contain the value directly, or have other C pointers to it. The first few fields of the struct contain type information, and determine how to interpret the rest.

After a = b, both variables will point to the same struct.

answered Aug 26, 2020 at 17:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.