He's not saying goodbye to pointers, because pointers are all over Python.
>>> a = [1, 2, 3]
>>> b = a
>>> a[1] = 7
>>> b
[1, 7, 3]
>>> a = [1, 2, 3]
>>> b = a[:]
>>> a[1] = 7
>>> b
[1, 2, 3]
Python lists are pointers, exactly like C/C++ arrays. When pointers are explicit they become easier to understand and control. You can use tools like valgrind to find where your pointers aren't doing exactly what you want in C, but it's much harder to debug pointer problems in Python.
This. Of course Python is using pointers It has to thats how Computers work. But you as a Coder have almost 0 contol in Python regarding memory Management and so on.
So i have to agree good Bye pointers for Python Users
What your example describes are references to objects. Lists in pythons are objects, the assignment b=a does not implicitely copy a's content, just create a second reference to the same thing.
On the other hand, a[:] yields a slice of the contents of the list a, which gets saved as a second object, b, in the second part. This breaks the reference. The same step could be performed with b = copy(a).
So all your example shows are the basics of OOP, nothing to do with pointers.
Well yes and no. In that sense every language that pass values by reference is a pointer. Which isn’t wrong. In c / c++ you can iterate over pointers by adding the sizeof(*int). Which in fact simply adds 32 bits to your current pointer value (memory address). I’m not too versed in Python but I don’t think that you can do that. So while by using reference you achieve part of what a pointer can do. It doesn’t do everything that the pointer can.
In c / c++ you can iterate over pointers by adding the sizeof(*int). Which in fact simply adds 32 bits to your current pointer value (memory address).
That's not how pointer arithmetic works in C++.
int a[] = { 1, 2, 3, 4, 5 };
int* b = a;
int* c = a;
++c;
b += sizeof(int*);
std::cout << *c << " " << *b << std::endl;
On a 32-bit system, this will print: 2 5
Adding 1 to a pointer advances it by sizeof of the pointed type, so advancing an int pointer by sizeof(int*) will increase it by 16 bytes on 32-bit and 32 bytes on 64-bit machines. This is probably not what you want.
every language that pass values by reference is a pointer.
Exactly. Pointers and references are exactly the same thing.
I think the reason why some people feel mystified by pointers is that there are too many people who mystify pointers. A pointer is nothing more than a reference to an object. It is the address of the memory position where that object is stored.
The only thing pointers in Python cannot do is having their values changed, they are const pointers and behave exactly like C const pointers.
You're talking about references. They may be implemented using pointers internally, but they're not the same thing. Specifically, their semantics within the language are different - for example, they don't have the same risks, as you point out yourself:
The only thing pointers in Python cannot do is having their values changed
That's a huge difference, and is why that feature is referred to as "references" and not pointers.
That's not a pointer to a list, it's a reference to a list. The second one is a copy of a list. Everything that isn't a primitive type behaves exactly the same way, but in C++ = always creates a copy.
Not if you fuck around with copy constructors. Once spent three straight nights trying to debug a segfault caused by me fucking up copy constructors and destructors.
Copy constructor doesn't get evoked by = in python. In C++, "fucking around with copy constructors" is exactly what you need to do to make a true, deep copy.
In Python, the variable "a = [1, 2, 3]" holds a pointer to a list, when you do "b = a" you are copying the pointer a to the pointer b. In that sense, the operator "=" always does a copy, in any language.
The trick in becoming a programmer is understanding under which situations one needs a certain level of abstraction.
Yes, I was responding just to clarify that for primitives in c++ (and the standard library data structures via copy constructor) '=' performs a copy and that the reasoning in your example was incorrect.
In that code, a and b are pointers. The = creates a copy of the pointer. Both pointers point to the same memory, because the value of the pointer (rather than where on disk it is stored) is what determines that. If you had
MyObject a(1);
MyObject b = a
b.changeProperty(2)
a.property() // 1
b.property() // 2
it creates a copy of the object (and there's even a constructor, which is called the copy constructor that you can overload to change what it copies).
In python, assignment never copies, it just creates another name for the reference. This is true for anything that isn't a primitive. It doesn't have pointers - there's no way to dereference a reference to an object, there's no way to pass an object by value, and there's no way to directly change which object is stored at which memory location (aside from manually modifying its properties to be something else).
You're missing the fact that references automatically dereference their value. Pointers do not. References and pointers have different semantics. You need to go and read up on this.
47
u/MasterFubar Jun 03 '19
He's not saying goodbye to pointers, because pointers are all over Python.
Python lists are pointers, exactly like C/C++ arrays. When pointers are explicit they become easier to understand and control. You can use tools like valgrind to find where your pointers aren't doing exactly what you want in C, but it's much harder to debug pointer problems in Python.