[Tutor] TypeError in class destructor
Walter Prins
wprins at gmail.com
Sat Dec 10 20:22:10 CET 2011
Hi Mark,
On 10 December 2011 17:54, Mark Lybrand <mlybrand at gmail.com> wrote:
>
> I am working on the Files chapter of Dive into Python 3, and have implemented the example script at the end of this message. The first input prints to the terminal as expected, the second value prints to the file as expected. Then the script tries to destroy in the class instance and bombs with:
>
> TypeError: __exit__() takes exactly 1 positional argument (4 given)
> Exception ValueError: 'I/O operation on closed file.' in <_io.TextIOWrapper name
> ='out.log' mode='w' encoding='utf-8'> ignored
>
> and the final input is, naturally, never printed.
>
> Is the example wrong, or is this something to do with how Windows handles stdout that is causing this not to work as designed? I am using Python 3.2 on Windows Vista Home Premium.
It seems the example may be wrong -- the __exit__ method, as stated by
the error, is being given 4 parameters whereas the one defined in the
code only expects one. I've looked an this is correct on Python 3.2
that I have on Windows as well. Perhaps the implementation of
__exit__ has been changed somewhere and had the paramters added and
the book is just out of date? In any case, changing the def __exit__
line to:
def __exit__(self, type, value, traceback):
... will fix the problem.
Cheers
Walter
More information about the Tutor
mailing list