1

I'm making a very simple to-do list application and I'm trying to create a list for each instance of my class. I was looking to have seven lists, one for each day, which would be used to store tasks however I can't get my head around it. Example of what I am trying to do:

class Day:
    def __init__(self, name, todo_list):
        self.name = name
        self.todo_list = todo_list 

day_Mon = Day('Monday', (MonList=[]))
day_Tue = Day('Tuesday', (TueList=[]))
...

I am very new to OOP and I'm just doing this to try try improve my understanding so I'm not sure if this is even possible/sensible (I've only seen questions about creating lists of instances). Is anyone able to help?

4
  • Is there a particular error you're running into? Also, you don't need to wrap your second argument (the list) in parenthesis. Commented Jan 16, 2017 at 21:12
  • 3
    @leaf: no, he's not. (MonList=[],) would be a tuple. Here parenthesis are just useless. Commented Jan 16, 2017 at 21:12
  • The trailing comma denotes a tuple when there is only one item. Commented Jan 16, 2017 at 21:13
  • (1) you can remove he parenthesis from around your second argument to Day()'s constructer (2) You don't need to do MonList=[] or TueList=[]. Just simply pass in a list: day_Mon = Day('Monday', []) and the same for every other day. Commented Jan 16, 2017 at 21:16

3 Answers 3

1

How about something like this?

class Day:
    def __init__(self, name, todo_list=None):
        self.name = name
        if todo_list:
            self.todo_list = todo_list
        else:
           self.todo_list = []


day_Mon = Day('Monday', ['errands', 'study'])
day_Tue = Day('Tuesday',['laundry', 'cook'])
day_Wed = Day('Wednesday')

The constructor accepts two arguments name and todo_list. Notice that todo_list has a default value of an empty list. So the only mandatory argument to create this object is a name - day_Wed is an object where the todo_list attribute is initialized an empty list. It is common when writing classes to have mandatory and optional arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

I've edited your answer because you should not use a mutable object as a default argument. See here for an explanation.
1

Your initializer can require a name parameter and allow an optional todo_list that defaults to an empty list.

For reasons you should learn about but I won't go in to here, you should never use an empty list as a default value of a function parameter, so we'll use None and then set the desired value in __init__.

>>> class Day:
...     def __init__(self, name, todo_list=None):
...         self.name = name
...         self.todo_list = todo_list if todo_list else []

>>> day_Mon = Day('Monday')
>>> day_Mon.todo_list
[]
>>> day_Tue = Day('Tuesday', ['wash clothes', 'pack'])
>>> day_Tue.todo_list
['wash clothes', 'pack']

Comments

0

You could store the 7 days in a list or dictionary, but its common in OOP to define a container class that provides useful accessors. Here is one that lets you address days of the week by case-insensitive name or number (assuming Sunday is 0).

import datetime

class Day:
    def __init__(self, name, todo_list):
        self.name = name
        self.todo_list = todo_list 

class Week:

    weekday_names = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
        'Friday')
    weekday_index = {name.lower():idx 
        for idx,name in enumerate(weekday_names)}
    weekday_index.update({idx:idx for idx in range(7)})

    def __init__(self):
        self.days = [Day(name, []) for name in self.weekday_names]

    def __getitem__(self, name_or_ord):
        if isinstance(name_or_ord, str):
            name_or_ord = name_or_ord.lower()
        return self.days[self.weekday_index[name_or_ord]]

    def __setitem__(self, name, day):
        raise ValueError("Cannot set days in week")

week = Week()
week['Sunday'].todo_list.append('dinner with parents')
week['Monday'].todo_list.append('laundry')
print("Monday's tasks:", 
    ', '.join(week['monday'].todo_list))
print("Today's tasks:", 
    ', '.join(week[datetime.date.today().weekday()].todo_list))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.