[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

printing to stdout

On 19Aug2018 18:10, richard lucassen <mailinglists at lucassen.org> wrote:
>On Sun, 19 Aug 2018 10:11:08 -0400
>Joel Goldstick <joel.goldstick at gmail.com> wrote:
>> Your allusion to pointers is misguided.  Python is not like C or
>> assembler.  You don't, and don't need to know where objects are
>> stored.  Names are assigned to reference data objects
>I'll have another look at it, I was just searching for a clear
>explanation, but the page I found was not clear enough for me. I'll
>have to take some time for it...

If you think of references like C pointers you are 80% of the way there.

In CPython (the primary Python implementation, which you're using), a reference 
is effectively a pointer to the referenced Python object. _Internally_. And the 
result of the id() function (giving an object's identity) _happens_ to be a 
memory address.

But the language specification doesn't talk about pointers or memory addresses, 
and other implementations may use other abstractions to associate variables 
with the objects they reference.

So never talk about Python references as pointers. You'll cop noisy criticism!

Just think of them as arrows from a variable (or attributes, etc) to an object.

Every Python variable is a reference. So when you pass a variable to a function 
or use it in an expression, your putting a reference there. This:

  x = 3
  y = x

results in x and y referring to the same "3" int object. For ints this is 
pretty irrelevant, since they're immutable. Arithmetic works as you would 

But this:

  values = [1, 2, 3]
  values2 = values

Here, values and values 2 both refer to the same list - values2 does not have a 
copy. So:

  values2[1] = 4

means that values[1] and values2[1] are both 4 because it is the same list.

Cameron Simpson <cs at cskk.id.au>

Draw little boxes with arrows.  It helps.       - Michael J. Eager