🌐
Python Data Science Handbook
jakevdp.github.io › WhirlwindTourOfPython › 03-semantics-variables.html
Basic Python Semantics: Variables and Objects | A Whirlwind Tour of Python
So in Python, when you write ... you are essentially defining a pointer named x that points to some other bucket containing the value 4. Note one consequence of this: because Python variables just point to various objects, there is no need to "declare" the variable, or even require the variable to always point to information of the same type!
🌐
Reddit
reddit.com › r/learnprogramming › in python, what's the difference between a variable, an object, a reference, and a value?
r/learnprogramming on Reddit: In Python, what's the difference between a variable, an object, a reference, and a value?
November 17, 2022 -

I've been discussing Python semantics with a friend who's helped me a lot in the past with understanding programming concepts and he keeps saying that a Python variable is "a name bound to a reference" and "the object on the other side of the reference has a type and a value" while the variable itself doesn't. He's been trying to explain what he means, but I'm not just not fully understanding, so I'm hoping someone here can explain in a way that will make more sense to me.

I think part of why I'm struggling is that I'm very comfortable with the idea of variables and pointers in C because I first learned programming in C++ from a professor whose examples were very C-style, and because I completed an 8 month firmware internship which was primarily low-level c programming. So at this point, the idea of a variable as being fundamentally linked to a physical memory location is kind of stuck in my head. I tend to think of C variables as just labels for memory addresses and, in CPython at least, I know a Python variable's ID (obtained via the id function) IS just the memory address (that's what the CPython documentation says at least). But I also know that if I do something like,

x = 5
print(id(x))

x = 50
print(id(x))

it will print out two different values. My friend said that's because the id doesn't really belong to the variable itself, but to the object. So is the first ID number that would be printed by the above code then a reference to the ID of the object 5? But then, if 5 is an object, what's the value?

Top answer
1 of 3
4
What is a variable? A variable is a name that is bound to a non-null pointer to an object (a PyObject*, in CPython). Critically, None is also a non-null PyObject*. What is an object? An object is a PyObject whose data has been allocated on the heap. Crucially, a PyObject might exist for some time after nothing is pointing to it; it takes time for the garbage collector to step in and free the memory. What is a value? A value is the collective state of an object's members. In other words, in C, a PyObject is a struct, and the value of the object is the data inside all of the various members that it could contain. Now, let's look at your example. You are allocating memory for a new PyLongObject on the heap, and x now points to that object. Calling id(x) returns the contents of that pointer (a location in memory). Next, you allocate a new integer PyLongObject on the heap, and x now points to that object. The old object no longer has any references pointing to it, and will just sit there orphaned until the garbage collector frees it. What's the value? The value is the collective state of the members of that PyLongObject, whatever the implementation actually to define the data inside the object. In CPython, it's an array of uint32_ts.
2 of 3
3
So at this point, the idea of a variable as being fundamentally linked to a physical memory location is kind of stuck in my head. Yeah, I think you should unlink this idea. This is not a universally true statement, since different languages will have fundamentally different "concepts" or "models" of memory. This in turn means that the definition of a "variable" will change from language to language. I think this is easiest to explain by comparing and contrasting C with Python Lower-level languages like C fundamentally conceptualizes memory as a giant array of bytes split into two parts: the stack and the heap. Whenever you create a variable, you are reserving a small chunk of bytes on this "stack". You can store small quantities of data here -- things like ints, floats... If you want to store larger quantities of data, you typically do so on the heap. You ask your runtime (e.g. your operating system) permission to use a certain range of bytes (e.g. via malloc) and stick your data there. But in order to actually use this data, you now need to keep track of which specific index within this giant array your data starts at. We call these "memory addresses". And where can we store these indexes? Well, typically within one of your variables on the stack. So when we are doing a pointer dereference (e.g. *my_variable), what we are doing is: Looking at the specific chunk of bytes on the stack associated with my_variable. Reading the data located within those bytes Interpreting those bytes as an index within this giant bytes array. Grabbing the data starting at that index. Of course, everything I described above is a giant over-simplification and a lie. For example: Most modern-day operating systems don't literally represent memory as a giant array of bytes and instead split them up into discrete chunks called "pages". This enables them to do things like swap pages in and out of disk to allow programs to use more RAM then is physically present on the machine. This in turn means that the "memory address" is also a bit of an illusion. You are typically being given a "virtual memory address" which your operating system will under the hood map to a "physical memory address". This mapping will change as data is paged in and out, since where your data is literally stored on your physical RAM can change over time. Your variable does not necessarily need to correspond to something on the stack. Your compiler may opt to not bother storing any data there, and instead use your CPU's registers instead. This is done for efficiency purposes, since reading/writing to a register is faster then reading/writing to RAM. Most modern-day CPUs do a lot of clever caching whenever you read data from RAM. Instead of literally fetching data byte-for-byte from RAM, it pre-emptively fetches a whole bunch of bytes at once and stores it within a cache within your CPU. This helps speed up code that tries reading bytes in sequential order -- and can lead to unexpected slowness in code that doesn't. C's model by itself wouldn't give you any way of predicting this behavior. That said, you can mostly get away with ignoring all of the above implementation details. C's model and abstractions are consistent and self-contained and is sufficient for understanding how to write well-formed C programs. Higher-level programming languages like Python have a very different model. Python does still sort of have the concept of a stack and a heap, but that's about where the similarities with C end. In particular, unlike C Python does not have any concept of memory being a giant array nor any concept of memory having an address. Instead, it conceptualizes the stack as being literally the stack data structure (with push/pop operations), where each entry in the stack is a key-value map (e.g. a hashmap or dictionary). A variable is just a key within this dict. You can actually modify this stack frame dict directly. For example, try running the following from the Python IDE. >>> my_variable = 3 >>> stack_frame = locals() >>> stack_frame["my_variable"] = "surprise" >>> print(my_variable) surprise The locals() builtin function will return a reference to the current stack frame dict. We can then modify that dict directly to change out what our variable happens to be referring to. (Does this mean we can dynamically create new variables? Yes -- try doing stack_frame["brand_new_variable"] = 101; print(brand_new_variable)) Python's concept of a "heap" is even more abstract. In Python, the heap is just a space where objects can be stored. These objects are not stored at a particular memory address nor are in any particular order: the heap is basically one giant bag of stuff. These objects are given a unique integer id. You can imagine that's how the stack frame dict is keeping track of each object under the hood: a stack frame is a mapping of strings (variable names) to object ids, and the heap is a mapping of object ids to the underlying object. This is the mechanism by which a Python variable refers to an object. But I prefer to conceptualize this more visually. To me, a variable in Python is an arrow with a name that points to some object floating in the aether. Thinking about what's happening in terms of these "object ids" is honestly a bit too cumbersome to be useful in practice. And what exactly is an object? Well, either: A glorified wrapper around a dict, for user-defined classes Or special "primitive" object that the Python interpreter creates on your behalf Just like with C, this model is a gross oversimplification of what's actually happening under the hood. And like C, this model is internally consistent and self-contained, so you mostly don't need to worry about the implementation details. Granted, Python's model does break down a bit more frequently then C's does. For example, you do need to mentally consider how everything maps to the underlying physical memory if you care about performance. You also need to start caring about C's model if you want to write Python wrappers around C code. I know a Python variable's ID (obtained via the id function) IS just the memory address (that's what the CPython documentation says at least) This is just an implementation detail. It's true today, but CPython is under no obligation to continue using the memory address as the unique ID in the future. Similarly, other implementations of Python may choose to use something completely different. (That said, it's unlikely in practice that CPython will stop using the memory address as the unique id any time soon. Switching to something else would probably make it harder to implement the model we discussed above.) My friend said that's because the id doesn't really belong to the variable itself, but to the object. Yup, this is absolutely correct. So is the first ID number that would be printed by the above code by then a reference to the ID of the object 5? No: what's printed is literally the id of the object 5. If we want to be more precise, here's what's happening when we do print(id(x)): Python evaluates the expression x. To do this, it looks at the current stack frame dict, looks up the key "x", then grabs the object id -- a reference to an object representing the integer 5. Let's say for the sake of argument that this object id is 3017463234928. The x variable is evaluated to this object reference. The id(...) function accepts this object reference -- the 3017463234928 object id. It does some Magic™ and creates a new int object that represents the number 3017463234928. (Why does it bother creating an object? Well, because Python's model states that every value the user interacts with must be represented as an object). Anyways, the 3017463234928 int object is stored on the heap. Let's say this new int object has an id of 4999584263006. The id(x) expression evaluates to this object reference. The print(...) function accepts the 4999584263006 object id. It uses that to look up the underlying object, reads it, and prints out 3017463234928 to stdout. Visually, I guess this ends up looking sort of like this: +--------------------------+ | object id: 3017463234928 | x -------------> | type: int | | value: 5 | +--------------------------+ +--------------------------+ | object id: 4999584263006 | id(x) ---------> | type: int | | value: 3017463234928 | +--------------------------+ It's a bit weird to have id(x) point to something -- it's not a variable, after all. But hopefully you get the idea. But then, if 5 is an object, that what's the value? This is a bit easier to explain using custom objects instead of Python's builtin ones. Consider the following: >>> class Url: ... def __init__(self, domain, path): ... self.domain = domain ... self.path = path ... >>> >>> a = Url("reddit.com", "/r/learnprogramming") >>> b = Url("reddit.com", "/r/learnprogramming") >>> c = Url("reddit.com", "/r/aww") This is creating three separate Url objects: two representing reddit.com/r/learnprogramming and one representing reddit.com/r/aww. But how many distinct "entities" or "values" do we have? Arguably, just two. The a and b variables may be pointing to two distinct objects, but what they fundamentally represent is the same. So, we say those two objects have the same "value": the same high-level "meaning" or "data". But then, if 5 is an object, that what's the value? The "value" is the integer 5. Python will represent this integer using an int object.
Discussions

If everything is an object in Python, are variables objects too? - Stack Overflow
From 'Introduction to Computer Science Using Python' by Charles Dierbach : Local variables are automatically created (allocated memory) when a function is called, and destroyed (deallocated) when ... More on stackoverflow.com
🌐 stackoverflow.com
What are Variables and Objects and the difference between them?
Variables are identifiers/names for objects. https://docs.python.org/3/glossary.html#term-object object: Any data with state (attributes or value) and defined behavior (methods). Also the ultimate base class of any new-style class. More on reddit.com
🌐 r/learnpython
5
6
July 20, 2022
Are all variables in Python some object of a class? Or are they classes themselves? - Stack Overflow
I am a complete beginner to programming so this may be a ridiculous or simple question. I am trying to understand exactly what a "method" is. The standard answer is that it is a function More on stackoverflow.com
🌐 stackoverflow.com
python - where to declare object variables - Stack Overflow
If it's not possible to set a proper ... like object(). Of course, the above doesn't apply to @property attributes, but even those will generally have an underlying "private" attribute that should be set in __init__. For more info about properties, please see Raymond Hettinger's excellent Descriptor HowTo Guide in the Python docs. As juanpa.arrivillaga mentions in the question comments, we don't actually declare variables in ... More on stackoverflow.com
🌐 stackoverflow.com
March 7, 2017
🌐
Python Morsels
pythonmorsels.com › pointers
Variables and objects in Python - Python Morsels
February 28, 2022 - Unlike many programming languages, variables in Python are not buckets which "contain" objects. In Python, variables are pointers that "point" to objects.
🌐
Medium
medium.com › @143.saran › what-is-a-variable-object-class-in-python-d3854db695ce
What is a variable, object, class, functions in python— various data types and their conversion | by Anurag Saran | Medium
May 14, 2023 - VARIABLE — in general it is a ... which the data is stored. Definition: “A Python variable is a symbolic name that is a reference or pointer to an object....
🌐
Practicaldatascience
practicaldatascience.org › notebooks › PDS_not_yet_in_coursera › 20_programming_concepts › vars_v_objects.html
Python: Variables vs. Objects — Practical Data Science with Python
In Python, variables and the objects they point to actually live in two different places in your computer. As a result, in Python, it’s best to think of variables as pointing to the objects they’re associated with, rather than being those objects.
🌐
i-Programmer
i-programmer.info › programming › python › 11683-programmers-python-variables-objects-and-attributes.html
Programmer's Python - Variables, Objects and Attributes
April 2, 2018 - Programming book reviews, programming tutorials,programming news, C#, Ruby, Python,C, C++, PHP, Visual Basic, Computer book reviews, computer history, programming history, joomla, theory, spreadsheets and more.
🌐
iBiblio
ibiblio.org › swaroopch › byteofpython › read › class-and-object-vars.html
Class and Object Variables
There are two types of fields - class variables and object variables which are classified depending on whether the class or the object owns the variables respectively. Class variables are shared in the sense that they are accessed by all objects (instances) of that class.
🌐
Bham-carpentries
bham-carpentries.github.io › 2018-12-17-bham_python-novice-inflammation › 02-variables › index.html
Programming with Python: Types, Objects, Values and Variables
December 20, 2018 - In this case, the Object is the ... be who’s laptop it was, e.g. Fred’s laptop. In Python, we can assign an object or value to a variable, using the equals sign =. For example, to assign value 60 to a variable weight_kg, we would execute:...
Find elsewhere
🌐
Quora
quora.com › In-Python-what-is-the-difference-between-an-object-and-a-variable-A-couple-of-examples-might-help
In Python, what is the difference between an object and a variable? A couple of examples might help. - Quora
Answer (1 of 3): In Python, everything is an object. OK, what does that mean? Simply that every thing Python programmers manipulate–data, variables, functions, classes, instances of classes–exists in memory. Let’s try an example by creating an integer variable: [code]x = 1 [/code]I tell ...
🌐
Readthedocs
basic-python.readthedocs.io › en › latest › basic_objects.html
Basic Python Objects, Variables, and Operators — basic python 1.0.1 documentation
Everything in python can be considered to be either a variable, an object, or an operator. An object can be everything from a number, to a function, to something more complex like a class. For the moment let’s not worry too much about objects, in fact most of this course is about how to create ...
🌐
Real Python
realpython.com › python-variables
Variables in Python: Usage and Best Practices – Real Python
April 15, 2026 - In this syntax, you have the variable’s name on the left, then the assignment (=) operator, followed by the value you want to assign to the variable at hand. The value in this construct can be any Python object, including strings, numbers, lists, dictionaries, or even custom objects.
🌐
Stack Overflow
stackoverflow.com › questions › 42644341 › where-to-declare-object-variables
python - where to declare object variables - Stack Overflow
March 7, 2017 - If it's not possible to set a proper value for some attribute in __init__ then it should be set to an appropriate default value, eg an empty string, list, etc, None, or a sentinel value like object(). Of course, the above doesn't apply to @property attributes, but even those will generally have an underlying "private" attribute that should be set in __init__. For more info about properties, please see Raymond Hettinger's excellent Descriptor HowTo Guide in the Python docs. As juanpa.arrivillaga mentions in the question comments, we don't actually declare variables in Python.
🌐
Readthedocs
python-textbok.readthedocs.io › en › 1.0 › Variables_and_Scope.html
Variables and scope — Object-Oriented Programming in Python 1 documentation
Recall that a variable is a label for a location in memory. It can be used to hold a value. In statically typed languages, variables have predetermined types, and a variable can only be used to hold values of that type. In Python, we may reuse the same variable to store values of any type.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-variables
Python Variables - GeeksforGeeks
Python variables store references to objects, not the actual values themselves. When a variable is reassigned, it starts referencing a new object while the old unreferenced object becomes eligible for garbage collection.
Published: 2 weeks ago
🌐
Reddit
reddit.com › r/askprogrammers › what is the difference between the object, the variable and the value in python?
r/AskProgrammers on Reddit: what is the difference between the object, the variable and the value in Python?
March 14, 2022 -

hi guys. i want to ask something.

in this code, which one is the object, the variable, and the value? Im kinda confused abt the differences between them.

x = 'Robert'
print (x)
Robert

thanks in advance for the help!

Top answer
1 of 2
2
Variable and Value use a slightly different refrence frame than the term 'Object'. In the simplest sense, in x = 'Robert' x is your variable, a name which refers to an implicit 'box' in which you can place a value, which here is the string 'Robert'. An object is not something that's relvan within that more abstract refrence frame. To highly oversimplify, there is the often-cited "Everything in Python is an ojbect." Now, this is mostly used to refer to cool things you can do with functions. But it's relevant here in a more direct fashion. In a very rough sense, an object is an ordered amalgm of bits that, when interpreted correctly, represent something that probably contains some data, and potentially functions relevant to it. Such an object is specifically an instance of a class, which is something that desrcibes how such instances should look in general. Now, in other languages, various primitive types, for example, might not be objects, but in Python, they all are. (For example, calling dir(1) shows us that the int type implements various functions and variables belonging to the object, despite an int seemingly being a 'simple' type.) This has a lot of very usefull implications, from a simple matter of uniformity, to the mentioned things you can do with functions, to some things you can but probably shouldn't do with objects, but at the point you're at, it's not really relevant yet. Sorry for the "Object" explanation being a bit long-winded and inconclusive, it's the kind of thing you just start understanding once you've gotten used to Object-oriented programming a bit.
2 of 2
1
Variables hold value. So in this case, x is a variable, and 'Robert' is the value stored in that variable. "Object" is a broad term. As Rhoderick said, everything in Python is an object. 'Robert' is an object whose type is "string" and whose value is 'Robert'. x, being a variable, currently has the same type and value, but its type and value can change. true is an object whose type is Boolean.
🌐
Medium
medium.com › @prathik.codes › objects-vs-variables-a7c53a9ac3ec
Objects vs Variables. Life with Python #6 | by Prathik C | Medium
November 9, 2024 - We can also get this information ... 'int'> x.__class__ # <class 'int'> ... A variable is essentially a name or label that refers to an object stored in memory....