Python internals

P

Peter Anderson

Hi! I am slowly teaching myself Python. I was reading David Beazley's
excellent book "Python - Essential Reference"; in particular about
variables. Let me quote:

"Python is a dynamically typed language in which names can represent
values of different types during the execution of a program. In fact the
names used in the program are really just labels for various quantities
and objects. The assignment operator simply creates an association
between a name and a value. This is different from C, for example, in
which a name (variable) represents a fixed size and location in memory..."

As an old mainframe programmer, I understand the way C does things with
variable but this text got me wondering how Python handles this
"association" between variable name and value at the lower level. Is it
like a fifo list?

If there is any Python guru that can help I would be most interested in
your thoughts.

Regards,
Peter
--
Peter Anderson

There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things -- Niccolo Machiavelli, The
Prince, ch. 6
 
J

Jan Claeys

Op Tue, 15 Jul 2008 09:29:58 -0500, schreef Larry Bates:
Names are pointers in Python that point to values in memory.

But "pointers" doesn't have the same meaning as in "C" here.

Memory in Python is not (necessarily) an "array of bytes"; how & where
the values are stored in "physical memory" at any time during the
lifetime of an object is an implementation detail.

Maybe you can compare it to how a (university) library works: you ask a
librarian for a book named "The Taste Of Man by Slavenka Draculić" and
they fetch it for you, using whatever internal classification/retrieval
system they use.
 
T

Terry Reedy

Peter said:
Hi! I am slowly teaching myself Python. I was reading David Beazley's
excellent book "Python - Essential Reference"; in particular about
variables. Let me quote:

"Python is a dynamically typed language in which names can represent
values of different types during the execution of a program. In fact the
names used in the program are really just labels for various quantities
and objects. The assignment operator simply creates an association
between a name and a value. This is different from C, for example, in
which a name (variable) represents a fixed size and location in memory..."

As an old mainframe programmer, I understand the way C does things with
variable but this text got me wondering how Python handles this
"association" between variable name and value at the lower level. Is it
like a fifo list?

No. Names are associated with (references to) objects in a namespace.
The details are implementation specific. For CPython, the references
are addresses stored in an array. If the set of names is dynamic, names
are converted to indexes by hashing during execution and the array of
references is expanded before it gets full. If the set of names is
static, as is usual for the local namespace of functions, an
optimization converts the names to indexes (in a constant length array)
during compilation. (On function startup, slots corresponding to
non-parameter local vars are, I expect, initialized to invalid reference
values to detect 'use before assignment' bugs.)

Terry Jan Reedy
 
M

MRAB

Hi!  I am slowly teaching myself Python.  I was reading David Beazley's
excellent book "Python - Essential Reference"; in particular about
variables.  Let me quote:

"Python is a dynamically typed language in which names can represent
values of different types during the execution of a program. In fact the
names used in the program are really just labels for various quantities
and objects. The assignment operator simply creates an association
between a name and a value. This is different from C, for example, in
which a name (variable) represents a fixed size and location in memory..."

As an old mainframe programmer, I understand the way C does things with
variable but this text got me wondering how Python handles this
"association" between variable name and value at the lower level.  Is it
like a fifo list?

If there is any Python guru that can help I would be most interested in
your thoughts.
They're stored in a dictionary (hash table), called a "dict" in
Python, the name being the key and the value being a reference to the
actual value. The CPython implementation is written in C (hence the
name) and implements references with pointers.
 
P

Peter Anderson

Ben said:
The term "pointer" carries much extra baggage for a programmer
thinking of C (as the original poster is)...

Thanks everyone! Just a quick correction - "as the original poster is"
is a bit of a jump that does not reflect my original question. I DO
understand how C and other programming languages handle variables
internally (the bits of actual memory reserved, etc. etc.) and that's
why I asked the question in the first place.

If Python doesn't do it like C and the others then what mechanism does
it use - it's the sort of issue that helps me understand how the
language is interacting with the underlying operating system/hardware.

By way of background, in my ancient working days I looked after
mainframe systems written in COBOL and Natural (and some assembler which
I never had to support personally but my staff did). I found that most
programmers write bad code because they don't understand what the
machine is doing with their code. Probably doesn't matter any more but
old habits die hard! ;-)

Regards,
Peter
--
Peter Anderson

There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things -- Niccolo Machiavelli, The
Prince, ch. 6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top