Message139918
Using the "with" statement wrongly results in a confusing error message.
Code (originally written by Alex Gaynor):
class Timer(object):
def __enter__(self):
self.start = time.time()
def __exit__(self, exc_type, exc_val, tb):
print "Section time: ", time.time() - self.start
#Note the error here, I call the class, not an instance
with Timer:
pass
------------------------
Compare the Python 2.6 error:
ismael@chaos:~/Escritorio$ python bad.py
Traceback (most recent call last):
File "bad.py", line 8, in <module>
with Timer:
TypeError: unbound method __enter__() must be called with Timer instance as first argument (got nothing instead)
Against Python 2.7:
ismael@chaos:~/Escritorio$ python2.7 bad.py
Traceback (most recent call last):
File "bad.py", line 8, in <module>
with Timer:
AttributeError: __exit__ |
|
Date |
User |
Action |
Args |
2011-07-06 03:58:03 | Ismael.Garrido | set | recipients:
+ Ismael.Garrido |
2011-07-06 03:58:03 | Ismael.Garrido | set | messageid: <1309924683.37.0.310972531328.issue12503@psf.upfronthosting.co.za> |
2011-07-06 03:58:02 | Ismael.Garrido | link | issue12503 messages |
2011-07-06 03:58:02 | Ismael.Garrido | create | |
|