python equivalent of wantarray() : newbie

Alex Martelli aleax at aleax.it
Wed Sep 12 08:04:26 EDT 2001


"Karthik Gurumurthy" <karthikg at aztec.soft.net> wrote in message
news:mailman.1000286203.3187.python-list at python.org...
>
> please bear with me, am still learning.
>
> is there a way to determine whether a return value is expected from a
> method?
> like the way wantarray() works in perl.

No, because Python accepts just about anything the method
wants to return.  Perl is very context-driven, Python is
not: whatever object the method wants to return, including
None, will typically do, since all objects are first-class.

It's possible that _operations_ *later* applied on the
returned value may imply certain "desires" regarding said
value, but that doesn't really change the context-free
operation of Python.


> perl can determine the type of the variable used
>
> @array = call()//so it's list context so call can return a list
> $var = call()//scalar context so call can return say a string.
>
> But can python determine the same?? i guess it can't and that's why we
don't
> have a wantarray() equivalent in python??

Right.  Even if you could magically determined that an indexing
call()[something] was later to be applied, this STILL wouldn't
tell you whether one 'wants' a list or a string -- both are quite
able to be indexed (and so are dictionaries, other built-in
objects such as arrays, and of course many user-defined classes).
So, again, things ARE basically context-free.


Alex






More information about the Python-list mailing list