Modifying Class Object
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Wed Feb 10 18:11:10 EST 2010
On Wed, 10 Feb 2010 23:02:27 +0100, Alf P. Steinbach wrote:
> For a less likely more technical interpretation, as far as I know in
> Python there's just one case of a pointer that does not point to
> anything, namely as exemplified by
>
> def foo():
> print( x )
> x = "whatever"
>
> The Java equivalent would say "NullPointerException", in Python it says
> something like "unbound". Which means the same.
Not in plain language. NullPointerException means that x is assigned to a
null pointer, and you have tried to follow that pointer. Unbound means
that x has nothing assigned to it. Semantically, the difference is
somewhat analogous to the situations:
"the president is dead, and a new president hasn't yet been appointed"
(the office of the president is currently assigned to a dead guy)
versus
"the king has dissolved parliament and with it the office of president"
(there is no president at all)
Now, of course we understand that in the implementation of CPython, local
variables are stored efficiently in slots in an array, and you can't have
empty slots: every address always has some bit pattern. It is very likely
that the CPython implementation uses a nil pointer to indicate an empty
slot just like Java does. The difference is that Java exposes that
implementation decision as part of the language specification, while
Python deliberately does not.
It isn't an accident that the Python exception describes the *semantics*
of the error (x is not bound to any value) rather than the implementation
detail (the slot known as x has a nil pointer in it). Semantically, the
Python language treats the slot as empty and leaves the details of how
the implementation recognises empty slots as an implementation decision.
I'm just as interested by the implementation decisions made in CPython as
the next guy, but they don't effect the semantics of the high level
Python language. Here is a thought experiment for you:
One can imagine an implementation of Python that eschews all pointers for
some scheme by which objects are cleverly copied and synchronized as
needed. Such an implementation would still be Python, but it would be
entirely pointer-free.
--
Steven
More information about the Python-list
mailing list