Can Python function return multiple data?

Steven D'Aprano steve at pearwood.info
Thu Jun 4 18:16:33 EDT 2015


On Fri, 5 Jun 2015 03:26 am, random832 at fastmail.us wrote:

> On Thu, Jun 4, 2015, at 09:47, Steven D'Aprano wrote:
>> In other words, according to this Java philosophy, following `x = 23`,
>> the
>> value of x is not 23 like any sane person would expect, but some
>> invisible
>> and unknown, and unknowable, reference to 23.
> 
> Well, no, because, in Java, if the type of x is int, then the value
> really is 23. If it's Integer, then it's a reference to a boxed Integer
> object. Which is (thankfully) immutable. But isn't invisible, unknown,
> or unknowable at all.

I was talking about Python, hence the use of Python syntax <wink>

In Java, primitive types are passed by value, but objects are not, and to
the best of my knowledge, what I say below about Python also applies to
Java objects.


> Of course, in CPython, the type of an object reference is PyObject *.
> Which isn't invisible, unknown, or unknowable, either.

Well, I just tried to see that reference, so I would know what it is:

py> PyObject
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'PyObject' is not defined

Well, let me try another way:

py> x = 23
py> reference(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'reference' is not defined

Maybe it's a method?

py> x.getreference()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'getreference'

I tried another ~hundred~ ~dozen~ few possibilities, and they all failed
just as above. It seems that there is no way to see that object reference
from Python code, or know what it is. Hence, it is invisible, unknown and
unknowable.

What is visible and known is the *object itself*.

You have made the same mistake as the Java people: caring more about the
underlying implementation details rather than what's going on at the level
of Python code.


> If the value really were 23, the "is vs ==" problem wouldn't exist.

What "problem"? "is versus ==" is not a problem, it is a feature. The two
operators do two different things.

But I am *astonished* that you think that the value of x after x = 23 is not
23. If it's not 23, then what is it?

(Actually, I'm not that astonished. There are some kinds of foolishness that
only really smart people can fall for.)

Remember, you've tried to claim that it is not invisible or unknown, so you
must be able to see and know that value. So what is the value?


> x = 500
> y = x+1
> z = y-1
> 
> x and z (probably) point to two different objects.

Maybe they do, maybe they don't. That's not specified by the Python
language, and is an implementation detail. Other languages may make other
promises. Either way, that's irrelevant to the question at hand.




-- 
Steven




More information about the Python-list mailing list