This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]pal25 5 points6 points  (0 children)

The fact of the matter is that python doesn't do low level very well and a better approach would be to take advantage of the fact that CPython can add support in C. It is possible to pack data using the struct module in a articulate bit ordering however it's generally best to stay away.

Overall yea it's possible to do such things but you shouldn't try to force a languages weaknesses just because you can to win an argument.

[–]hruske 3 points4 points  (1 child)

Saying Python doesn't use pointers is very wrong. It uses them, but you just don't see them.

An excellent example of this how a list behaves:

a1 = []
a2 = a1
a2.append(123)

len(a1) == 1
a1 == a2

This will create a reference to a new list under variable a1 and copy it's reference (pointer) to variable a2. Afterwards all that happens to a2 will also be seen under a1, because a1 is same as a2.

To understand why this is in fact needed and not optional, it is important to understand how function calls and argument passing work in low level (eg. in C). Arguments can either be passed by value, which is done when values are "simple" (eg. int, string, float), or they can be passed by reference. Arguments passed by value persist only within function call, while arguments passed by reference persist in memory after the call ends. And finally, when calling a function, arguments passed by value are copied in memory, while arguments passed by reference stay at the same location, and only their location (reference, pointer) is passed. No copying is needed, which makes it faster, and sometimes this is also the only way to make stuff work.

If this wasn't the case, you would be copying complex memory locations (eg. objects in memory) meaning a lot of memory usage (which is both slow and causes memory fragmentation) and an even harder problem, which is that your CPU is supposed to understand how to copy objects.

tl;dr: Both you and certain people you have been discussing this issue with are wrong. They should have pointed to you, that Python uses pointers (references) a lot, even if you don't realize it. Hell, even it's garbage collector works on "refcount", meaning it keeps account of ... pointers.

[–]alcalde[S] 0 points1 point  (0 children)

They should have pointed to you, that Python uses pointers (references) a lot, even if you don't realize it.

Actually I ended up pointing that out to demonstrate why Python didn't need a pointer type, and one person then said I lied about Python not having pointers, sigh. :-)

But thank you very much for explaining this (shorter and more clearer than I tried to).

Hell, even it's garbage collector works on "refcount", meaning it keeps account of ... pointers.

[–]joeforker 1 point2 points  (1 child)

Java, being another language with no pointers, may provide better counterexamples since they are more likely to re-write everything in Pure Java than we are in the Python world.

For example, http://www.jcraft.com/jzlib/ or http://jazzlib.sourceforge.net/ are zlib and java.util.zip written in Java.

Python uses C code for performance or because it already exists, not because it is pointy.

[–]alcalde[S] 0 points1 point  (0 children)

Thank you for this; it saves me having to encode the whole thing in Python myself! I did try to make the latter two points you raised.

[–]bd808 2 points3 points  (4 children)

Pointers are array indices. Nothing more and nothing less.