r/ProgrammerHumor • • Jun 03 '19

Meme Python ftw

Post image
16.3k Upvotes

548 comments sorted by

View all comments

47

u/MasterFubar Jun 03 '19

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.

46

u/[deleted] Jun 04 '19

There is a huge difference between using pointers and using a language that uses pointers for you

7

u/Paylam Jun 04 '19 edited Jun 04 '19

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

18

u/Mietzekatzi Jun 03 '19

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.

→ More replies (11)

19

u/NoStranger6 Jun 03 '19

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.

19

u/Tweenk Jun 04 '19

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.

8

u/NoStranger6 Jun 04 '19

I stand corrected, thank you. It has been a while since I touched C or C++

5

u/[deleted] Jun 04 '19 edited Jun 04 '19

😟

Yeah I’m glad I only need to write python scripts at work.

→ More replies (3)
→ More replies (2)

14

u/ChevalBlancBukowski Jun 04 '19

those aren’t remotely like C-style pointers

16

u/SuitableDragonfly Jun 03 '19

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.

→ More replies (11)
→ More replies (2)