[python-win32] return & tuples (& lists)

Larry Bates larry.bates at websafe.com
Mon Jun 30 03:30:36 CEST 2008


Michel Claveau wrote:
> Hi!
> 
> Not a question. Only a little note...  (for readers without Sunday 
> activity)
> 
> 
> In (pure) Python, consider this code:
>    def ftest():
>        vret=(111,222,333)
>        return(vret)
> 
>    print ftest()     #give:  (111, 222, 333)
> 
> 
> In (COM) Python, the same method of the class of a dynamic-COM-server 
> give other return:
>    def ftest():
>        vret=(111,222,333)
>        return(vret)
> 
>    pv = win32com.client.Dispatch('.......
>    print pv.ftest()     #give:  111
> 
> 
> 
> In (pure) Python, this code:
>    def ftest():
>        vret=(111,222,333)
>        return(vret,)   #note the comma!
> 
>    print ftest()     #give:  ((111, 222, 333),)
> 
> 
> In (COM) Python, the same method of the class of a dynamic-COM-server 
> give other return:
>    def ftest():
>        vret=(111,222,333)
>        return(vret,)  #note the comma!
> 
>    pv = win32com.client.Dispatch('.......
>    print pv.ftest()     #give:  (111, 222, 333)
> 
> 
> Another detail:
>    def ftest():
>        vret=[111,222,333]  #list in the place of tuple
>        return(vret)
> 
>    pv = win32com.client.Dispatch('.......
>    print pv.ftest()     #give:  (111, 222, 333)
> 
> 
> And, to finish: in COM,  return(vret) & return(vret,)  give the same 
> result.
> 
> 
> 
> I wish you marvellous Sunday, with sun, bathe and aperitif.

Misunderstanding about return values:

 >    def ftest():
 >        vret=(111,222,333)
 >        return(vret)


This is the same as: return vret because the outer () are discarded as they are 
superfluous.  You could write return ((((vret)))) and it would do the same 
thing.  It still gets intrepreted as return tuple(111,222,333).

COM obviously treats things differently.

If you have a tuple and want to return it, just return it:  return vret

If you want to wrap objects in a tuple to return it, I would recommend using the 
build in tuple() command as the parenthesis can be confusing:  return 
tuple(111,222,333).

The reason return (vret,) works is because it is the same as: return 
tuple(tuple(111,222,333) (the trailing comma is the key here and is what tells 
python you mean a tuple instead of the "normal" parenthesis meaning.

May seem inconsistent, but I don't believe it really is.

-Larry



More information about the python-win32 mailing list