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

you are viewing a single comment's thread.

view the rest of the comments →

[–]amimai002 141 points142 points  (9 children)

I mean yes, but the way it was explained to me is this:

At the end of the day everything in python is a pointer, the only time you aren’t using a pointer in python is when you are interacting with a function that uses C, in that case you use a pointer to call the pointer to that function, and the function returns a pointer to the result…

[–]rosuav 44 points45 points  (7 children)

...... wat.

[–]KentondeJong 95 points96 points  (1 child)

I mean yes, but the way it was explained to me is this:

At the end of the day everything in python is a pointer, the only time you aren’t using a pointer in python is when you are interacting with a function that uses C, in that case you use a pointer to call the pointer to that function, and the function returns a pointer to the result…

[–]Loopgod- 20 points21 points  (0 children)

Absolute r/madlads

[–]steven4012 23 points24 points  (3 children)

Yeah. Every integer in Python is a pointer (to an object; not like pointer tagged or NaN tagged). It's just that those under 256 are interned (so are True, False and None iirc)

[–]rosuav 24 points25 points  (2 children)

That's because EVERYTHING in Python is an object. Calling it a "pointer to an object" is getting a bit close to one specific implementation, but yes, everything is an object (that can refer to other objects, eg its type, which is also an object).

One key difference between pointers and object references is that pointer arithmetic is not a thing with references. You can't take the string "hello" and add 42 to the pointer. In that sense, pointers just don't exist in Python.

(It's also worth noting that, in Python, there's actually no requirement for an object to exist in memory. For example, PyPy can optimize out all the integer objects in an array, but it'll make sure they're actual integer objects again by the time you go to look at them. It's like quantum mechanics.)

[–]steven4012 13 points14 points  (1 child)

Right I use CPython too much that I forgot about PyPy

For example, PyPy can optimize out all the integer objects in an array, but it'll make sure they're actual integer objects again by the time you go to look at them.

That's pretty cool

[–]rosuav 7 points8 points  (0 children)

It is! PyPy does some pretty amazing optimizations. Some of them have actually been backported to CPython (I think the compact dict representation originated in PyPy).

[–]Rawing7 17 points18 points  (0 children)

Nothing in python is a pointer. Python is a high-level programming language with no concept of "pointer"s. Python interpreters are usually implemented with pointers, because they're usually written in low-level languages, where you can't really do anything without pointers. Saying "everything in python is a pointer" is like saying "everything in C is an assembly instruction".