IsString

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Dec 13 17:33:10 EST 2005


On Tue, 13 Dec 2005 23:21:02 +0100, Xavier Morel wrote:

> Steven D'Aprano wrote:
>> name = "spam spam spam spam"
>> 
>> the value of the variable "name" is a pointer, and not a string. Riiight.
>> 
> Yes, it's a reference to an object of type string holding the value 
> <spam spam spam spam>

The underlying C implementation might be that, but we're not talking about
the underlying C implementation, where talking about the Python level.
Python does not have pointers. If you want to go down the path of
treating Python as if it were the underlying implementation, I'll just
argue that everything in Python is untyped bytes, because that's the way
the machine code executed by the CPU sees things.


>> 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.
>> 
> 
> That example is mightily flawed since Python's integers are immutable 
> objects.

That's the *reason* why the behaviour doesn't work. But the *fact* that it
doesn't work is enough to prove that Python is not call by reference. Call
by reference *demands* the ability to do that sort of thing.

> Now use a mutable type instead of an immutable int and you'll notice a 
> pass-by-reference behavior.

Wrong. Will this example clarify things?

def modify_in_place(obj):
    """Modify an arbitrary object in place."""
    obj = None

x = [1, 2, 3] # mutable object
modify_in_place(x)
assert x is None


Doesn't work either.

Sometimes call by object behaves like call by reference, and sometimes
like call by value, but it is not either of them.



-- 
Steven.




More information about the Python-list mailing list