top | item 20197803

Pointers in Python

114 points| gilad | 6 years ago |realpython.com | reply

22 comments

order
[+] stinos|6 years ago|reply
Article is mostly fine, but the author does some effort to mention CPython-specific things, but not consistently, making it a bit of a confusing read for when you've worked with different Python implementations at the implementation level (C in my case). E.g. Each object contains at least three pieces of data: Reference count well, in CPython yes but not in implementations which use mark and sweep GC.
[+] randomvectors|6 years ago|reply
Somewhat unrelated to the article, but I'm curious - how commonly and for what purposes are other implementations used for?
[+] Thorrez|6 years ago|reply
I think restype should be set to None in addition to setting argtypes. That way you can avoid the weird number being printed out:

    >>> add_one(ctypes.byref(x))
    998793640
[+] analog31|6 years ago|reply
I don't think there's a reason why support for pointers can't be added as a package, perhaps one that integrates with ctypes. It could function like PEEK and POKE in the old BASIC interpreters. However, accessing storage as a base plus offset, if desired, could be implemented with numpy arrays.
[+] ForHackernews|6 years ago|reply
Aren't all variables in Python already somewhat like a pointer? They're a reference to an object, not a memory address containing a value.
[+] amelius|6 years ago|reply
Yes, but pointers are more basic, since Python's internal references participate in garbage collection.

If you would use pointers exclusively, then you'd have to write your own garbage collector. Also, accessing data would be more difficult, and you could not use Python's data types such as classes.

[+] frankbreetz|6 years ago|reply
Perhaps this is a novice question, but what is an instance where one would need to use pointers in python? I am having trouble thinking of an instance where I would need to do this.
[+] Thorrez|6 years ago|reply
If you want to call a C function that takes a pointer as an argument. For example I've called a Windows API function from python which does that.
[+] bigiain|6 years ago|reply
Whiteboarding a doubly linked list at an interview for a job that mentions Python in the requirements... <smirk>
[+] tinalumfoil|6 years ago|reply
> Fact: Values live until nothing references them.

So if I write

    circular_linked_list = { "next": None }
    circular_linked_list["next"] = { "next": circular_linked_list }
    circular_linked_list = None
My linked list won't get cleaned up? I think the author should be more careful with his wording.
[+] scardine|6 years ago|reply
IINM the reference implementation has a cache for small integers. I wonder what happens if you try the ctypes example in the article with one of the cached integers.
[+] scardine|6 years ago|reply
Nothing happens with the cache. According to the article you can't use a Python int, instead you have to use a ctypes.c_int and those are not cached.
[+] tells|6 years ago|reply
ah yes! this is where i read about string interning. I totally had forgotten the source. Thanks!