4 ms·
Pointers in Python
- scardine 7y agoIINM 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 7y agoNothing 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.
- knome 7y agoThis is an excellent question with a generally fun answer, at least in python 2. https://kate.io/blog/2017/08/22/weird-python-integers/ https://kate.io/blog/2017/08/22/weird-python-integers/ http://hforsten.com/redefining-the-number-2-in-python.html http://hforsten.com/redefining-the-number-2-in-python.html I found these offhand, but I'm pretty sure I saw this trick earlier than 2017.
- analog31 7y agoI 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.
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- frankbreetz 7y agoPerhaps 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 7y agoIf 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.
- ehsankia 7y agoSure, but you only need to because the C function requires you to. I think the person was saying that while coding pure Python, there isn't really anything you would gain by having pointers.
- Thorrez 7y agoYeah, for pure python it's not really needed. Python doesn't need a swap function because it has multiple assignment, python doesn't need output parameters because it has multiple return. Pointers would allow a function to take in a string and replace it with a modified version, but simply returning the modified version would generally be clearer and more flexible. One place where pointers might be useful is if you have some strings spread across a bunch of different objects of different types and you want to do some transformation on all of them. A pointer to each of those strings would mean you could simply modify them instead of having to assign to the containing object. But you can sort of define your own pointer type with a class with one field to solve that problem. The downside of that would be all the objects would have to contain that pointer type instead of the string directly. Another alternative would be getattr/setattr (although that wouldn't work for locals). Or maybe you could rewrite to a visitor pattern. Overall the slight benefit a pointer would provide is outweighed by the complexity it would bring of people wondering how and when to use it.
- ignaloidas 7y ago
- svat 7y agoRelated reading (perhaps worth reading as a precursor to this), from Ned Batchelder: • Facts and myths about Python names and values https://nedbatchelder.com/text/names.html https://nedbatchelder.com/text/names.html and its presentation version: • Python Names and Values https://nedbatchelder.com/text/names1.html https://nedbatchelder.com/text/names1.html
- Thorrez 7y agoI 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
- tells 7y agoah yes! this is where i read about string interning. I totally had forgotten the source. Thanks!
- stinos 7y agoArticle 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 7y agoSomewhat unrelated to the article, but I'm curious - how commonly and for what purposes are other implementations used for?
- stinos 7y agoMicroPython [0] for instance gets mainly used on microcontrollers. Hard to tell how common it is, but it definitely gained some traction, also runs on the micro:bit [1] which gets used for teaching. Which isn't surprising: Python is easier for beginners than C. i don't know about other implementations though. [0] https://github.com/micropython/micropython/ https://github.com/micropython/micropython/ [1] https://www.microbit.org/ https://www.microbit.org/
- ForHackernews 7y agoAren'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 7y agoYes, 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.
- tinalumfoil 7y ago> 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.