WorldDominator WorldDominator - 5 months ago 22
Python Question

Python List of classes

I'm trying to design a "Time Tracker" device. I want to be able to define a class line like:

class line():
def __init__(self, course, weekHours, hoursTotal, comment)
self.course = course
self.weekHours = weekHours
self.hoursTotal = hoursTotal
self.comment = comment

Then be able store an array (I guess it's called a list in Python?), of these class objects. So I can print a table produced by all of these lines and save these lines to an output file and then later be able to read that back into this list to view the table or make changes. do I declare
table = [class line()]
? If so, how do I access each of these objects in the list? I want to be able to differentiate them so I can edit a particular "line" if necessary.


You can store class instances in a list:

lines = []
lines.append(line('Math', '3', '12', 'Hello World!'))

To get the i'th line, you'd just do:


Note that there really isn't a good reason to have a class here. a python dict would be more efficient:

lines = []
lines.append({'course': 'Math', ...})