I have a class that has few static fields and is initialized from iterable (like output from csvreader
). The __init__
performs type conversion from strings to numbers for some of them:
class PerformanceTestResult(object):
def __init__(self, csv_row):
# csv_row[0] is just an ordinal number of the test - skip that
self.name = csv_row[1] # Name of the performance test
self.samples = int(csv_row[2]) # Number of measurement samples taken
self.min = int(csv_row[3]) # Minimum runtime (ms)
self.max = int(csv_row[4]) # Maximum runtime (ms)
self.mean = int(csv_row[5]) # Mean (average) runtime (ms)
self.sd = float(csv_row[6]) # Standard deviation (ms)
I’m thinking about converting it to be just a namedtuple
, as there is not much else to it. But I would like to maintain the type conversion during initialization. Is there a way to do this with namedtuple
? (I haven’t noticed __init__
method in the verbose output from namedtuple
factory method, which gives me pause about how the default initializer works.)