My question relates mostly to how you use the with
keyword in a class in Python.
If you have a Class which contains a file object, how do you use the with
statement, if at all.
For example, I don't use with
here:
class CSVLogger:
def __init__(self, rec_queue, filename):
self.rec_queue = rec_queue
## Filename specifications
self.__file_string__ = filename
f = open(self.__file_string__, 'wb')
self.csv_writer = csv.writer(f, newline='', lineterminator='\n', dialect='excel')
If I then do things to the file in another method, For example:
def write_something(self, msg):
self.csv_writer(msg)
Is this suitable? Should I include the with
somewhere? I'm just afraid that one the __init__
exits, the with
exits and might close the file?