IsString

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Dec 13 16:47:04 EST 2005


On Tue, 13 Dec 2005 15:28:32 +0000, Tom Anderson wrote:

> On Tue, 13 Dec 2005, Steven D'Aprano wrote:
> 
>> On Mon, 12 Dec 2005 18:51:36 -0600, Larry Bates wrote:
>>
>> [snippidy-doo-dah]
>>
>>> I had the same thought, but reread the post.  He asks "if a given 
>>> variable is a character or a number".  I figured that even if he is 
>>> coming from another language he knows the difference between "a given 
>>> variable" and the "contents of a give variable".  I guess we will 
>>> see.... ;-).  This list is so good, he gets BOTH questions answered.
>>
>> The problem is, Python doesn't have variables (although it is 
>> oh-so-tempting to use the word, I sometimes do myself). It has names in 
>> namespaces, and objects.
> 
> In what sense are the names-bound-to-references-to-objects not variables?

Because saying "Python has variables" leads to nonsense like the following:

[snip]
>> That's why, for instance, Python is neither call by reference nor call
>> by value, it is call by object.
> 
> No, python is call by value, and it happens that all values are
> pointers. 

All values in Python are pointers???

So when I write:

name = "spam spam spam spam"

the value of the variable "name" is a pointer, and not a string. Riiight.

Call by value and call by reference have established meanings in
computer science, and Python doesn't behave the same as either of them.
Consider the following function:

def modify(L):
    "Modify a list and return it."
    L.append(None); return L

If I call that function:

mylist = range(10**10) # it is a BIG list
anotherlist = modify(mylist)

if the language is call by value, mylist is DUPLICATED before being
passed to the function. That does not happen in Python. Consequently,
expected behaviour of a call by value language is that anotherlist and
mylist will be different. That is not true in Python either. So Python is
not call by value.

But neither is it call by reference. If it were call by reference, I could
write something like this:


def increment(n):
    """Add one to the argument changing it in place."""
    # In Pascal, I would need the var keyword to get this behaviour,
    # but Python is call by reference so all variables are passed 
    # by reference.
    n += 1

x = 1
increment(x)
assert x == 2

but that doesn't work in Python either.

So Python behaves demonstrably different from BOTH call by value and call
by reference. Consequently, it is neither of them.

The conceptual problem you are having is that you are conflating the
object model of Python the language with the mechanism of the underlying C
implementation, which does simply pass pointers around. But when
discussing the behaviour of Python, what counts is the behaviour of Python
code, not the underlying C mechanism -- which may be different than the
underlying Java mechanism in JPython, or Python mechanism in PyPython.


[snip]

> I'm sure this has been argued over many times here, and we still
> all have our different ideas, so please just ignore this post!

I'd love to, but unfortunately I've already hit send on my reply.



-- 
Steven.




More information about the Python-list mailing list