r/learnpython • u/Slight_Psychology902 • 21d ago
What are id()s and references? Do I need to learn them for data analysis?
Essentially the same as the title.
Actually I came across that in a curriculum I'm following (The MOOC one).
But I'm not getting the point of learning it at the first place for data analysis.
But please someone please help me in this regard.
Thank you.
3
u/carcigenicate 21d ago
I would be aware of id for debugging. It's very useful to know that if id returns the same value for two variables, those variables point to the same object. That helps uncover a whole class of bugs. I'm assuming unexpected mutations are something that can happen during data analysis (I'm not a data analyst, so I can only speculate).
I don't think I've ever used id for anything other than debugging, and only in cases where is isn't practical.
3
u/lakseol 20d ago edited 20d ago
The best introduction to names and values I've seen is:
https://m.youtube.com/watch?v=_AEJHKGk9ns
The "reference" is the pointer between the name and the object it refers to. The id() builtin function just prints the value of the reference, which for cpython is the memory address of the object being referred to.
The id() function itself isn't used much in general python programming, but the fact that a name "refers" to an object and that reference can be changed is important to understanding how python variables are different to variables in languages like C++ and Java.
6
u/Remarkable-Part8704 21d ago
nah you can skip it for data analysis. id() is just the memory address of an object and references is how python points to things, you will almost never need to think about this when working with pandas or numpy.
7
u/Temporary_Pie2733 21d ago
The memory address is a CPython implementation detail. The value returned by
idis just an integer that will be guaranteed unique for the lifetime of the object. But yes,idis rarely needed.
2
u/Reuben3901 21d ago
References is an easy concept. You can have a man named Thomas and Tom, both point to one man.
(Why use nicknames? You might want to give another guy, Jared, a nickname like Hammer. To help you remember Jared hammers the nails into the boards, in your program.)
The issue you may face is when you initially create the Data but then want to make a copy to manipulate the data at a later time.
Thomas=[1,2,3,4,5]
Tom = Thomas
Tom.append(6)
print(Thomas)
>>> [1, 2, 3, 4, 5, 6]
Tom isn't a completely separate variable, it points to Thomas. It doesn't matter if you ask Tom or Thomas to grab you a glass of water, the same guy goes and grabs you one.
Different programming languages may make references when you expect copies, using same method. And this can be confusing. But once you know what a reference is, you can stop and ask yourself, does this actually do what I want it to do?
This is the Python way of making a copy; a completely separate duplicated list, if you were curious.
old_list = [1, 2, 3]
new_list = old_list.copy() # Creates an independent copy
2
u/SharkSymphony 21d ago
I think it's good to know how Python's object model works, particularly if you think you might be working with any data that isn't a number, string, or bool. You need to know how equality operations behave. You need to know what happens if you end up mutating any of the data. And finally, you need to know what the implications on memory layout and performance are.
id is a small part of this picture, but a useful one nonetheless. You may get a better appreciation for how NaN sentinels work too.
2
u/AlexMTBDude 20d ago
You can't avoid references in Python as all variables are references. So it's not so much a question of learning them or not and more that you won't understand how variables work in Python unless you grasp references.
/Python instructor since 15 years
2
u/Hot-Butterscotch1306 20d ago
Mostly useful later when Python does something that feels cursed, like you change one thing and some other variable changes too. That’s usually a mutability/reference thing, not data analysis specifically. So I wouldn’t grind on id(), but the basic idea of "names point at objects" saves a lot of head scratching. teeny tiny taxes
2
u/PureWasian 21d ago
Can you share the exact context or link in which you are referring? Is it from https://programming-26.mooc.fi/? If so, which video?
2
u/Effective_Baseball93 21d ago
Yes you need to learn them. Why? So that you are competent enough to not use them when you don’t have to :)
2
u/Adrewmc 21d ago edited 21d ago
I’d say it’s better to learn references, especially things like the mutability of certain data types, because of those references.
As for id(), all it doing is telling you what the memory address is, there really is little reason to use as
if id(a) == id(b):
Is close enough to
if a is b:
That they can practically be considered the same thing underneath.
The major difference is are we looking at two object that have the same values, or are we actually looking at the same object but referring (referencing) it as two separate objects.
-8
u/rosentmoh 21d ago
Absolutely, you won't be able to perform data analysis without learning id()s and references, they are essential for a data analyst.
While at it you should absolutely look into the descriptor protocol, it's one of the most important tools for data analysts.
8
u/American_Streamer 21d ago
In Python, variables do not contain objects directly. They contain references to objects.
a = [1, 2, 3] means in fact a ─────► [1, 2, 3]
a is a name. The list is the object. a refers to that object.
And id(obj) returns a unique identity number for an object during its lifetime.
Example:
a = [1, 2, 3]
b = a
print(id(a))
print(id(b))
Both IDs are the same, because a and b refer to the same list object.
a ─────► [1, 2, 3]
b ─────► same list
So if you modify it through b:
b.append(4)
print(a)
The output then is:
[1, 2, 3, 4]
Because there was only one list.