[Python-3000] interaction between locals, builtins and except clause (original) (raw)
Eduardo "EdCrypt" O. Padoan eopadoan at altavix.com
Thu Jul 26 19:27:11 CEST 2007
- Previous message: [Python-3000] interaction between locals, builtins and except clause
- Next message: [Python-3000] interaction between locals, builtins and except clause
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 7/26/07, Lisandro Dalcin <dalcinl at gmail.com> wrote:
Porting to Py3K, I modified a function like the followin, using a trick for it working in Py2.x .
def iter(self): if self == mpi.INFONULL: return try: range = xrange except: pass nkeys = mpi.infogetnkeys(self) for nthkey in range(nkeys): yield mpi.infogetnthkey(self, nthkey) However, I've got in my unittests (running with py3k) ERROR: testPyMethods (main.TestInfo) ---------------------------------------------------------------------- Traceback (most recent call last): File "tests/unittest/testinfo.py", line 123, in testPyMethods for key in INFO: File "/u/dalcinl/lib/python/mpi4py/MPI.py", line 937, in iter for nthkey in range(nkeys): UnboundLocalError: local variable 'range' referenced before assignment
I am not completelly sure if this is expected (it is, regarding implementation, but perhaps not regarding Python as a language), so I post this for your consideration.
Python thinnks range is local, because you referenced it, even if an error ocurred. Use 'global range' at the top of the file.
But, as I understand, you are trying to target both Python 2.x and 3 with the same code, using tricks like this one. I think that, even if you succeed, the resulting code will be quite unmaintainable.
-- EduardoOPadoan (eopadoan->altavix::com) Bookmarks: http://del.icio.us/edcrypt
- Previous message: [Python-3000] interaction between locals, builtins and except clause
- Next message: [Python-3000] interaction between locals, builtins and except clause
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]