On Fri, 7 Jan 2005 19:40:18 -0800 (PST), Ilya Sandler <ilya at bluefir.net> wrote: > Eg. I just looked at xdrlib.py code and it seems that almost every > invocation of struct._unpack would shrink from 3 lines to 1 line of code > > ( i = self.__pos > self.__pos = j = i+4 > data = self.__buf[i:j] > return struct.unpack('>l', data)[0] > > would become: > return struct.unpack('>l', self.__buf, self.__pos)[0] > ) FWIW, I could read and understand your original code without any problems, whereas in the second version I would completely miss the fact that self.__pos is updated, precisely because mutating arguments are very rare in Python functions. OTOH, Nick's idea of returning a tuple with the new offset might make your example shorter without sacrificing readability: result, newpos = struct.unpack('>l', self.__buf, self.__pos) self.__pos = newpos # retained "newpos" for readability... return result A third possibility - rather than "magically" adding an additional return value because you supply a position, you could have a "where am I?" format symbol (say & by analogy with the C "address of" operator). Then you'd say result, newpos = struct.unpack('>l&', self.__buf, self.__pos) Please be aware, I don't have a need myself for this feature - my interest is as a potential reader of others' code... Paul.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4