Slices time complexity

Marko Rauhamaa marko at pacujo.net
Tue May 19 05:35:04 EDT 2015


Chris Angelico <rosuav at gmail.com>:

> Sorta-kinda-maybe, but if a C programmer's idea of pointers is invoked
> to explain Python's object references, the differences will start to
> be problematic:
>
> 1) Pointer arithmetic simply doesn't exist in Python. Arrays/lists are
> not just pointers to their first elements, and subscripting is most
> definitely NOT "add to pointer and dereference".

Barely an issue.

> 2) In fact, dereferencing as a whole isn't really a 'thing' either. At
> best, it happens automatically.

Yes, you could say to a C programmer: "Python's . corresponds to C's ->"
and be done with it.

> 3) References actually mean something. Copying a pointer doesn't.
> Whether the Python you're using is refcounted (CPython) or
> mark-and-sweep (uPy, I think) or some other model, an additional
> reference to the same object will prevent it from being disposed of,
> which isn't the case in C.

"Disposing of" shouldn't concern a beginning Python programmer. Note
that Scheme does not address the whole topic in its standard. The memory
model is infinite if you will.

> 4) A pointer is itself a value. You can pass a
> pointer-to-local-variable to another function and have that function
> change a local variable.

Correct, variables are not first-class objects in Python. In C, they
are. Functions are not first-class objects in C. In Python, they are.

Still, that doesn't make the pointer semantics any less applicable to
Python.

> 5) Furthermore, since a pointer is a value, you can have pointers to
> pointers, etc. Doesn't make any sense in Python.

What you are saying is that references in general are not first-class
objects in Python. In C, they are.

So in Python, we have these memory locations (variables, references)
that are accessible through special syntax only. Ok. Again, that in no
way invalidates pointer semantics. (And Guido could approve a perfectly
backwards-compatible PEP tomorrow that elevates references to the
first-class status.)

> As long as you are aware that analogies are always limited, you can
> certainly use them as part of an explanation; but you do have to take
> care.

I'm talking about a model, not a mere analogy.


Marko



More information about the Python-list mailing list