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 do it like with any other object in python... you don't need to declare anything.sashkello– sashkello2013年11月06日 06:21:53 +00:00Commented Nov 6, 2013 at 6:21
-
@sashkello Sorry I've been working in C for too long now that I forget how Python works exactly.WorldDominator– WorldDominator2013年11月06日 06:26:53 +00:00Commented Nov 6, 2013 at 6:26
1 Answer 1
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:
lines[i]
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', ...})
7 Comments
class Lines()? lines[i].course seems not to work.