Objects in Python

Chris Angelico rosuav at gmail.com
Sat Aug 25 02:34:50 EDT 2012


On Sat, Aug 25, 2012 at 1:04 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> You're confusing two different levels of explanation here. On the one
> hand, you're talking about C semantics, where you are explicitly
> responsible for managing unnamed data via indirection (pointers).
> Typically, the *pointers* get given names, the data does not.
>
> On the other hand, you talk about Python, where you have no access at all
> to the pointers and memory addresses. You manage the data you actually
> care about by giving them names, and then leave it up to the Python
> virtual machine to transparently manage whatever indirection is needed to
> make it work.
> ...
> * in C, I care about identifiers ("names") in order to explicitly manage
> addresses and pointers as a means to reach the data I actually care about;
>
> * in Python, I care about identifiers in order to reach the data I
> actually care about.

Yet the two are almost the same. Python objects don't have names, they
just have their own data. (Leaving aside functions, which have their
names as data for the benefit of tracebacks and such.) A C pointer has
a name; a Python identifier has (or is, if you like) a name. They're
very different in how you use them only because C doesn't naturally
work with everything on the heap and pointers everywhere. In fact,
when I was interfacing Python and C, there were a few places where I
actually handed objects to Python and kept manipulating them, simply
because the Python data model suited what I was trying to do; but what
I was doing was using PyObject *some_object as though it were a Python
variable. I even did up a trivial C++ class that encapsulated the
INCREF/DECREF work, so my LocalPyObject really could be treated as a
local variable, Python-style.

Where's the difference?

ChrisA



More information about the Python-list mailing list