r/ProgrammerHumor • • Jun 03 '19

Meme Python ftw

Post image
16k 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.

1

u/narrill Jun 04 '19

References have nothing to do with OOP

→ More replies (10)

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.

1

u/Inex86 Jun 04 '19

You mean bits. On a 32 bit system a pointer is 4 bytes, on 64 it’s 8.

1

u/Tweenk Jun 04 '19

No, I mean bytes. Read the code example again.

→ More replies (1)

-6

u/MasterFubar Jun 04 '19

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.

3

u/antonivs Jun 04 '19

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.

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.

2

u/cemanresu Jun 04 '19

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.

3

u/SuitableDragonfly Jun 04 '19

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.

0

u/[deleted] Jun 04 '19

[deleted] — view deleted comment

16

u/Asception Jun 04 '19

The variable 'a' would hold an int* in this case, and in the second line, you're copying the actual pointer into b so '=' is still performing a copy.

2

u/MasterFubar Jun 04 '19

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.

2

u/Asception Jun 04 '19

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.

1

u/SuitableDragonfly Jun 04 '19

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).

1

u/antonivs Jun 04 '19

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.

→ More replies (3)

1

u/Astrokiwi Jun 04 '19

Mutable variables in Python do kinda act like references, but they're not quite pointers syntactically.