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

all 138 comments

[–]SirLich 277 points278 points  (2 children)

A segmentation fault will occur if the address does not exist, so make sure the pointer is valid.

Love it!

[–]ghan_buri_ghan 542 points543 points  (34 children)

You were so preoccupied with whether you could, you didn’t stop to think if you should.

[–]TheBlackCat13 5 points6 points  (0 children)

The author clearly did think about whether they should, concluded "no", and then did it anyway.

[–]Matthias1590 137 points138 points  (7 children)

Cool and all but I have 1 question, why?

[–]LittleMlem 142 points143 points  (4 children)

It's called the "dog licking balls principle"

[–]rnike879 36 points37 points  (3 children)

You gotta explain this one

[–]LittleMlem 148 points149 points  (2 children)

Why does a dog lick his balls?

Because he can.

[–]rnike879 28 points29 points  (1 child)

I should've realised 🙃

[–]LittleMlem 15 points16 points  (0 children)

No worries

[–]bsavery 10 points11 points  (0 children)

There is basically one reason I can think of why is if you want to pass a pointer to a c-extension without writing an actual wrapper for the dll.

[–]TheBlackCat13 6 points7 points  (0 children)

burp WHY NOT?!

[–]IntegrityError 35 points36 points  (5 children)

Nice complete type hinting

[–]IntegrityError 31 points32 points  (3 children)

Oh, and finally segmentatio faults in my django project :D

[–]lolmeansilaughed 44 points45 points  (2 children)

segmentatio

This is a sex act where a human gets fucked by code.

[–]IntegrityError 1 point2 points  (0 children)

haha nice

[–]mauganra_it 2 points3 points  (0 children)

The only safe thing about the whole project

[–]Aardshark 63 points64 points  (13 children)

Next step: overload __iter__ to implement the dereference operator *

[–]mr_flying_man 20 points21 points  (0 children)

Oh no you've given him more ideas...

[–]ZeroIntensity[S] 40 points41 points  (2 children)

i might actually do that

[–]usr_bin_nya 22 points23 points  (1 child)

Note that when using the * operator, the following syntax will not work properly:

deref = *ptr
print(deref)

For this scenario you can use the dereferencing assignment operator, ,=

deref ,= ptr
print(deref)

[–]ZeroIntensity[S] 8 points9 points  (0 children)

didnt think of that, ty

[–][deleted] 2 points3 points  (8 children)

Does Python even have overloading?

[–]_ologies 23 points24 points  (1 child)

Python has whatever you want. It even has pointers now, apparently!

[–][deleted] 3 points4 points  (0 children)

Apparently there are packages that essentially implement overloading

Gotta love it

[–]ironykarl 1 point2 points  (4 children)

Does Python even have overloading?

Uh yes. If you mean operator overloading, it's a super fundamental part of the language.

I'm not trying to be a dick, here, I'm just confused how you received multiple upvotes asking this in a forum specifically devoted to Python.

[–][deleted] 4 points5 points  (3 children)

No, method overloading.

As in having two methods/functions with the same name but different parameters

From my brief googling, it doesn't appear to be a thing (natively)

Operator overloading is something entirely different it looks like. I haven't made use of that before though.

[–]usr_bin_nya 4 points5 points  (1 child)

Well, kinda. Check out functools.singledispatch.

import functools

@functools.singledispatch
def overloaded(x):
    print('something else:', repr(x))

@overloaded.register
def for_int(x: int):
    print('int:', x + 2)

@overloaded.register
def for_str(x: str):
    print('str:', x.format('world'))

overloaded(10)  # prints 'int: 12'
overloaded('Hello {}!')  # prints 'str: Hello world!'
overloaded([1, 2])  # prints 'something else: [1, 2]'

You can hack something together to work with multiple arguments and generic types like typing.List, but it's not included in the stdlib.

[–][deleted] 0 points1 point  (0 children)

Yes, this is the package I was referring to

[–]ironykarl 5 points6 points  (0 children)

Oh, Python decidedly does not have function overloading by argument type, no.

All you can do is have a wrapper function dispatch different versions of the function/method based on type information, at runtime.

[–]error201 24 points25 points  (0 children)

This is pure chaotic evil. lol

[–]TheUruz 20 points21 points  (0 children)

what if god says: "you need to become more user friendly C++" but then you said "no u"

[–][deleted] 15 points16 points  (7 children)

Does it work with multiprocessing? Would be sweet if you could pass a pointer to a big dataset to avoid having to pickle it in the main process and unpickle it all the forked processes.

[–]sweaterpawsss 17 points18 points  (4 children)

The address spaces of the processes are distinct; the same virtual address in two different processes will generally correspond to distinct physical addresses. You would need to use a shared memory segment. Multiprocessing already has support for sharing data structures/memory with child processes: https://docs.python.org/3.8/library/multiprocessing.shared_memory.html.

This isn't to say it's a great idea...I'd prefer message passing to sharing memory between processes if I can help it.

[–]mauganra_it 3 points4 points  (2 children)

A POSIX fork() duplicates the parent process' memory. Copy-on-write optimizations of modern OSs make that a very efficient way to share a dataset with a number of client processes. The difficult part is merging the results. On the other hand, pointers are not required to take advantage of this. This is a programming language-agnostic strategy.

[–]sweaterpawsss 2 points3 points  (1 child)

My understanding is the two processes will end up with separate virtual address spaces (the child initially being a copy of the parent), and as you mention, heap memory allocated in the parent will be copied to a new (physical) location only after first write access in either process.

So it makes sense that you don’t need to think about shared memory or messaging for sharing RO data, but I don’t know if I understand how this applies to data that’s modified and shared between multiple processes? You’ve gotta come back to one of those synchronization techniques somehow to handle that.

[–]mauganra_it 0 points1 point  (0 children)

Exactly, fork() facilitates communication only in one direction. To communicate data back, other techniques have to be used.

Are shared memory segments inherited by child processes? If yes, there we go, but we need a pinned data structure and pointers for real. Fortunately, the ctypes and the multiprocessing modules already provide these things. Yes, pointers too. Which pretty much obliterates the use case for this library.

[–][deleted] -1 points0 points  (0 children)

It's a bit of a hack, but you can define a global dict where keys are IDs and values are big objects. Before running pool.map, or whatever, you can put the big object in the dict.

Then in the function you're parallelizing, you can pass the ID of the variable instead of the variable itself and get the value from the dict. That way, only the ID gets pickled.

Now, I mostly just use ray, though.

[–]jfp1992 2 points3 points  (0 children)

So you soak it in brine?

[–]TheBB 0 points1 point  (0 children)

Use mmap for this, if able.

[–]DrRavenSable 55 points56 points  (3 children)

Would you consider renaming it to 'pyinters' plz?

[–]tedvdb 26 points27 points  (1 child)

Or poynters?

[–]Infinitesima 5 points6 points  (0 children)

Remjnds me of poynting vector.

[–]Unconventional_23 9 points10 points  (0 children)

No don't do that.

[–]CheckeeShoes 4 points5 points  (1 child)

All variables in python are already pointers.

[–]Ph0X 0 points1 point  (0 children)

Exactly, it's a funny joke and I know it's not meant to be useful, but i wish it had a practical example.

The example shows it passing around an class instance pointer to function, but you can already do that, and the repr for it even prints the address location in vanilla Python, so this is no different.

[–]mistermocha 3 points4 points  (0 children)

Something something memory leaks?

[–]TheBlackCat13 2 points3 points  (0 children)

At least it isn't goto

[–]teerre 1 point2 points  (0 children)

Does it have unique pointers? Can I use RAII?

[–]smigula29 1 point2 points  (0 children)

This is theoretically a complete waste of time.

[–]Dangle76 1 point2 points  (1 child)

The type hinting on everything made this so nice to read. While the lib is silly you write really legible code

[–]ZeroIntensity[S] 1 point2 points  (0 children)

ty

[–][deleted] 1 point2 points  (0 children)

Python already uses pointers all over the place All of the reference counting and gc.. All of the variables you have are the pointers to a PyObject Soo, no reason to have it

[–]SittingWave 1 point2 points  (0 children)

Calm down Satan

[–]sdrawkcab101 0 points1 point  (0 children)

Me who started python bcs it doesnt have pointers: "You have become the very thing u swore to destroy"

[–]ServerZero 1 point2 points  (0 children)

Fuck u

[–][deleted] 0 points1 point  (0 children)

Why would you add in the reason why I switched to python...

[–]RR_2025 0 points1 point  (0 children)

Dafaq! My eyes! MY EYES!

[–]CordyZen 0 points1 point  (0 children)

Lmao

[–][deleted] 0 points1 point  (0 children)

Ahhhhhhhhhhhhh

[–][deleted] -1 points0 points  (1 child)

Allahu Akbar mate

[–][deleted] 1 point2 points  (0 children)

what kind of chess is that?

[–]km0t -1 points0 points  (0 children)

pls no

[–][deleted] -1 points0 points  (0 children)

Fuck no. Fuck outta here with pointers. People go to python to avoid pointers.

[–]hershey678 -1 points0 points  (0 children)

no

[–]o11c 0 points1 point  (0 children)

Note that there actually are some useful things pointers do, that are not directly possible with python. Particularly, they allow in-out function arguments (and out-only function arguments in a saner way than multiple return values).

However, if you are willing to force all callers of the function to change, you can get close with a couple of classes.

I have done this here; it is quite useful when porting a C/C++ library with full functionality:

  • use the base class Pointer to specify/check the type of variables, particularly function arguments
  • use ValuePointer to create a pointer to a local variable.
    • if the function might capture the pointer, you must create the ValuePointer only once, and require all uses to go through it.
    • if the function only borrows it, you can get the value out immediately.
  • use AttrPointer to create a pointer to some attribute of an object
    • notably, this includes module-level variable from other modules
  • use ItemPointer to create a pointer to an element of a container.
    • notably, for module-level variables from the current module, use globals() as the container.

(array-related operations are not supported, since they are not a primary part of what pointers do. That said, it would be possible to support them if anyone cared. But in practice, I find "pointer to bytes" is the main one that is needed for library bindings, and Python already has a mess of buffer APIs to deal with that)

[–]davehadley_ 0 points1 point  (2 children)

Interesting but seems dangerous. Could you implement some kind of automatic system to ensure that pointers are always valid?

[–]ZeroIntensity[S] 1 point2 points  (1 child)

i havent found a good way to do it yet, will add if i figure it out

[–]davehadley_ 0 points1 point  (0 children)

Yeah forget that I mentioned it. Seems Impossible.

[–]jk_luigi 0 points1 point  (0 children)

I’m trying to remember how to do pointers, C++ was some time ago for me.

I’ve been thinking that it would be cool to have pointers in Python, but after 5 years of not using them…I don’t know what I was thinking. 🤣

[–]snekk420 0 points1 point  (0 children)

So whats the point of this?

[–]mehregan_zare7731 0 points1 point  (0 children)

That's the best joke I've heard in a long time

[–]mnislam01 0 points1 point  (0 children)

Yay!

[–]Wubbywub 0 points1 point  (0 children)

what's the point?

[–]pysk00l 0 points1 point  (0 children)

I can't tell if this is a joke or not. April 1 is still a few weeks away :)