I want to make a singleton class that gathers data from a csv file, and in order to do that, it needs to have a data member of type DictReader; but I am not sure how I would initialize this member in the class definition, as it can only be initialized like so:
with open('sourceFile.csv') as source:
reader = csv.DictReader(source)
Because Python will not allow you to declare variables without initialization, I need to know how I can initialize the reader object in the class Singleton.
Are you looking for something like:
class MySingleton(object):
def __init__(self, source):
self.my_reader = DictReader(source)
if __name__ == '__main__':
singleton = MySingleton(sourcefile)
for row in singleton.my_reader:
# do stuff
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.