I am running Python 3.6.3 in Spyder 3.2 and getting
Error:TypeError: object() takes no parameters
at with ManagedFile('hello.txt') as f:
class ManagedFile: def _init_(self, name): self.name = name def _enter_(self): self.file = open(self.name, 'w') return self.file def _exit_(self, exc_type, exc_val, exc_tb): if self.file: self.file.close() with ManagedFile('hello.txt') as f: f.write('hello there!') f.write('bye')Obviously ManagedFile isn't a function that excepts parameters. But according to the author, _enter_ is supposed to work with the
withstatement to enter its content. The last three lines are supposed to create a new file and write to it. The class is supposed to function as a "context manager," which is a new term to me. Is there something wrong you see in the code?