Is 'everything' a refrence or isn't it?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jan 6 10:49:06 CET 2006


On Thu, 05 Jan 2006 22:18:39 -0600, Terry Hancock wrote:

>> Consider this:
>> 
>> def do_nothing(x):
>>     pass
>> 
>> huge_tuple = (None,) * 10000**4
>> do_nothing(huge_tuple)
>> 
>> If Python made a copy of huge_tuple before passing it to
>> the function, you would notice.
> 
> Which succinctly demonstrates precisely why a newbie
> *should* be told that Python passes references instead of
> values. 

"I don't like Chinese food, and pizza gives me gas. Let's have a burger,"
says I.

And Terry helpfully answers: "Pizza gives you gas? Well there you go then,
that's precisely why we have to have Chinese food."

I have never suggested that we should tell newbies that Python is call
by value, and I deny that the only two possible choices for usefully
describing Python's behaviour is by CBV and CBR. The fact that Terry could
read this thread and still imagine that there is such a dichotomy is
worrying.


> But it should probably also be made clear that
> "reference" means a label assigned to an object, and not a
> variable containing a memory location (which is what a C
> "pointer" is).
> 
> The reference is usually a simple name, but it can also be
> an container expression following list, dictionary,
> or class instance (spam, spam[0], spam['eggs'], or
> spam.ham, for example).  Not tuple or string because they
> are immutable, and so don't have assignable references.

Are you seriously suggesting that you can't include tuples in a list, or
pass them to functions? Because that's what it sounds like you're saying:
"Python passes references. The reference is usually a simple name, but it
can also be a container... Not tuple or string because they are immutable."

If you don't mean to say that Python can't pass strings to functions, what
do you mean to say?

I'll tell you what I say: Python passes objects to functions or
assignments.

Does this mean that the object is copied? No, I didn't say it copies
objects. I left the nature of the passing mechanism unspoken, which is how
it should be because it is an implementation detail.

Does this mean that the object can be modified in place? Certainly not,
since that depends on the object, not on the nature of Python's high-level
behaviour. It emphasises the object oriented nature of Python, and by its
very language warns the reader not to assume that Python behaves
identically to other languages. It invites further questions, rather than
lulling the reader into jumping to false conclusions.

The emphasis is on the *object*, not the passing mechanism. In C,
everything is mutable, and whether you can change an item depends on
whether you are working with a reference to that item or a copy of the
item. That's a language issue, not a data issue -- in some languages,
you can even choose whether to pass a reference to a function or a copy
of the value.

In Python, whether or not you can change an object depends on the object,
not the language itself: it is a data issue.


-- 
Steven.




More information about the Python-list mailing list