The main change is that the file attribute is initialized to None instead of being conditionally set to an open file. Instead, the file is opened in the write_to_file method if it hasn't been opened yet. This makes the code more explicit and easier to reason about. Additionally, I added some whitespace and changed the indentation to conform to PEP 8 style guidelines.
class Solution: def __init__(self, data, filename='kumite776.txt', preopen=False): self.data = data self.filename = filename self.file = None if preopen: self.file = open(self.filename, 'w') def write_to_file(self): if not self.file: self.file = open(self.filename, 'w') self.file.write(self.data)
- class Solution:
- def __init__(self, data, filename='kumite776.txt', preopen=False):
- self.data = data
- self.filename = filename
self.file = open(self.filename, 'w') if preopen else None- self.file = None
- if preopen:
- self.file = open(self.filename, 'w')
- def write_to_file(self):
if not self.file: self.file = open(self.filename, 'w')- if not self.file:
- self.file = open(self.filename, 'w')
- self.file.write(self.data)