Strange Python 2.1 return style (was Re: . Python 2.1 function attributes)

Neil Hodgson neilh at scintilla.org
Sat Jan 27 16:15:23 EST 2001


Tim Peters wrote:
> [Roy Katz]
> ...
> > It seems to me even kludgier than print>>.
>
> Barry also added code to Python 2.1 to let you write, e.g.,
>
>     def f() >> x:
>         x = 1
>         y = 2

   Even though Tim was <wink>ing, I think there is some value here.

   Not so necessary in the above but I'd like it to be easy to see what is
being returned in complex tuples from functions like os.stat:

>>> os.stat("c:\\autoexec.bat")
(33279, 0, 2, 1, 0, 0, 27, 980627178, 944531984, 944229948)

   The argument list describes the inputs, but not the outputs:
def stat(path):
   could be

def stat(path) >> (mode, ino, dev, nlink, uid, gid, size, atime, mtime,
ctime):
   although putting the output description on the left would be better as it
mirrors the calling pattern
def (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) stat(path):

   Its unlikely that this will be embraced due to the already mentioned
difficulty in ensuring that the value returned does match the def statement,
although it could at least be checked for length by the compiler or runtime.

   IDEs such as PythonWin provide calltips with this information when you
are constructing a call. They do this by looking for a __doc__ convention.
The information needs to be present somewhere in the code documentation, so
making it a part of the def statement does not add to code bulk. It allows
simpler use (os.stat.__returns__) than trying to parse a doc string that may
or may not follow a convention.

   Neil





More information about the Python-list mailing list