coding style - where to declare variables
steve+comp.lang.python at pearwood.info
Mon Jul 23 07:39:36 EDT 2018
On Mon, 23 Jul 2018 09:22:55 +0300, Marko Rauhamaa wrote:
> Dennis Lee Bieber <wlfraed at ix.netcom.com>:
>> In my world, Java and Python are the ones that are not "common".
> Yes, "boxed" is a Java term. However, the programming pattern of using
> dynamic memory and pointers is ubiquitous and ancient:
Not that ancient -- the first version(s) of Fortran didn't have dynamic
memory allocation or pointers. (Admittedly, Lisp did follow not long
afterwards.) But it is certainly not ubiquitous: many languages don't
have pointers at all.
> FILE *f = fopen("xyz", "r");
> where f holds a pointer, fopen() returns a pointer, and "xyz" and "r"
> evaluate to pointer values.
> In Python, every expression evaluates to a pointer and every variable
> holds a pointer.
Within the semantics of the Python language, there are no pointer values,
no way to get a pointer to a memory location or a pointer to an object.
No expression in Python evaluates to a pointer, no variables hold
pointers in Python. The Python language is defined in terms of objects:
expressions evaluate to objects, and variables are names bound to objects.
If you don't believe me, believe the interpreter:
# Marko expects a pointer, but unfortunately he gets an int
py> type(1 + 2)
Marko is making a similar category error as those who insist that Python
uses "call by reference" or "call by value" for parameter passing. He
mistakes an irrelevant implementation detail used by *some* but not all
Python interpreters for entities which exist in the Python computation
model. As Fredrick puts it:
"Joe, I think our son might be lost in the woods"
"Don't worry, I have his social security number"
(The *pointer to an object* used in the implementation is not the same as
the object itself.)
Evaluating 1 + 2 gives the value (an object) 3, not a pointer to the
value 3. Pointers are not merely "not first-class citizens" of Python,
they aren't citizens at all: there is nothing we can do in pure Python to
get hold of pointers, manipulate pointers, or dereference pointers.
Pointers are merely one convenient, useful mechanism to implement
Python's model of computation in an efficient manner on a digital
computer. They are not part of the computation model, and pointers are
not values available to the Python programmer.
 The CPython interpreter uses pointers; the Jython interpreter uses
whatever kind of memory indirection the JVM provides; when I emulate a
Python interpreter using pencil and paper, there's not a pointer in sight
but a lot of copying of values and crossing them out. ("Copy on access"
perhaps?) A Python interpreter emulated by a Turing machine would use
dots on a long paper tape, and an analog computer emulating Python would
use I-have-no-idea. Clockwork? Hydraulics?
 Except by dropping into ctypes or some other interface to the
implementation, and even then the pointers have to be converted to and
from int objects as they cross the boundary between the Python realm and
the implementation realm.
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson
More information about the Python-list