anything like C++ references?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Fri Jul 18 11:23:06 EDT 2003


On Sun, 13 Jul 2003 22:20:33 +0100, rumours say that Stephen Horne
<intentionally at blank.co.uk> might have written:

>On 13 Jul 2003 15:05:38 -0500, Ian Bicking <ianb at colorstudy.com>
>wrote:
>
>>On Sun, 2003-07-13 at 14:39, Stephen Horne wrote:

>>> The fact is that 'assignment' has a common meaning separate from the
>>> choice of programming language, 

>>This just isn't true.  The C++ assignment operator is not at all like
>>the Python assignment statement.  Python variables are not like C++
>>variables, no surprise assignment is different too.  If you used
>>languages outside of C++ and its like (e.g., Pascal), you would find
>>Python's behavior common.
>
>Think again.
>
>When I say "'assignment' has a common meaning separate from the choice
>of programming language" I assumed you would get the hint. I'm not
>referring to some specific other programming language, of which I have
>used many - and not all of them imperative. I am referring to the
>definitions in computer theory, which do not relate to any specific
>programming language but actually apply to all of them, irrespective
>of paradigm and everything else.

[and snip of various other stuff]

perhaps I am too late, and off subject at this point in time, but I
believe I can offer something to the discussion; I just picked to follow
up a post that looked appropriate for my digression below.



The way I see it, practically, there are two ways to describe the python
variables/names, and it comes down to choosing labels (nb. the following
paragraphs are valid for CPython, and do not take account of other
computer languages or computer science in general):

- if you choose the labels 'name', 'object', 'binding' as most
pythonistas do, then the python assignment operator 'binds' 'names' to
'objects'.  You can't 'bind' 'names' to 'names', since they are not
'objects'.
The function id() returns the identity of the 'object' 'bound' to
'name'.
Argument passing is neither 'call by reference' or 'call by value'; it's
something one can name 'call by object'.

- if you choose the labels 'variable', 'value', 'reference', 'object',
then *all* 'variables' are of the same type: 'reference' to 'object'.  A
'variable' is not a python 'object', so you can't have one 'variable'
'referencing' another 'variable'.  You can't store an 'object' to a
'variable', just a 'reference' to the 'object'.
The function id() returns the 'value' of the 'variable'.
In this case, all argument passing is "call by value"; all 'values' are
'references' to 'objects'.



Please note that a python 'name' (or a python 'variable') is completely
described by the following regular expression:

^[_A-Za-z][_A-Za-z0-9]*$

So you are certain that you are 'binding' a 'name' to an 'object' (or
storing the 'reference' to an 'object' in a 'variable') only when you
have something like the following:

name = <any valid python expression>

This is *always* an assignment operation.  You can't control it with
specially-named functions (something like __assign__, say).  If you are
of the 'variable', 'value' persuasion, then most of the time the 'value'
of "name" changes by the assignment operation, even if the new 'object'
is equal to the old 'one'.

OTOH, if you have something like:

another_name.name = <any valid python expression>

then the LHS is not a 'name' (or a 'variable').  It's the "name"
attribute (or property) of the "another_name" object.  This code can
call several python functions, depending on the type/class of
"another_name".  The execution of the above statement might invoke some
methods of the 'object' presently known as "another_name" (eg
__setattr__ or a special property put method).
The "name" attribute can be a 'name' (or a 'variable'), but it's not
necessarily so.



This post is a good example of bad writing, but the ways of
enlightenment are obscure... ;-)
-- 
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.




More information about the Python-list mailing list