Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:18:36+00:00 2026-05-27T23:18:36+00:00

I have a class that stores information about a week: from django.db import models

  • 0

I have a class that stores information about a week:

from django.db import models

#A django model, so can't subclass list (at least not easily)
class MyWeek(models.Model):
    sunday    = "foo"
    monday    = "foo"
    tuesday   = "foo"
    wednesday = "foo"
    thursday  = "foo"
    friday    = "foo"
    saturday  = "foo"

I’d like to be able to access these attributes as if the class was a list:

aweek = Myweek()

#I want this
aweek[0] = "bar"
myvar = aweek[1]

#To be shorthand for this
aweek.monday = "bar"
myvar = aweek.tuesday

#and of course
aweek[7]
ValueError/IndexError: Week indexes monday to 0 and sunday to 6, there is no 7

Everything about python makes think this is possible and easy, if only I know the right set of things to overload.

I’ve thought of @property, but that doesn’t help so much because I want to be able to use a variable to access it:

#I want to be able to do this
aweek[somevar] = "bar"

#and via property, i'd have to use exec
#and this is just ugly and scary from an "oh god, what could somevar be" perspective
exec("aweek.%s = 'bar'" % somevar)

#Or, as kojiro pointed out below, it could be done like this:
setattr(aweek, "somevar", "bar")

Thanks.

Edit: Working code, hattip to kojiro for helping with the right methods to overload:

# overload []
def __getitem__(self, index):       
    index = int(index) #will raise value error if uncoercable, this is desired behavior
    if index < 0 or index > 6:
        raise ValueError("Requires an integer index between 0 and 6, monday is 0 sunday is 6")
    if index == 0:
        return self.monday
    elif index == 1:
        return self.tuesday
    elif index == 2:
        return self.wednesday
    elif index == 3:
        return self.thursday
    elif index == 4:
        return self.friday
    elif index == 5:
        return self.saturday
    elif index == 6:
        return self.sunday   

# overload set []
def __setitem__(self, index, item):
    index = int(index) #will raise value error if uncoercable, this is desired behavior
    if index < 0 or index > 6:
        raise ValueError("Requires an integer index between 0 and 6, monday is 0 sunday is 6")
    if index == 0:
        self.monday = item
        return
    elif index == 1:
        self.tuesday = item
        return
    elif index == 2:
        self.wednesday = item
        return
    elif index == 3:
        self.thursday = item
        return
    elif index == 4:
        self.friday = item
        return
    elif index == 5:
        self.saturday = item
        return
    elif index == 6:
        self.sunday = item
        return
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:18:37+00:00Added an answer on May 27, 2026 at 11:18 pm

    To create a list-like object in python you need to create the following methods:

    __len__, __getitem__, __setitem__, __delitem__, __iter__, and __contains__

    Link to explanatory example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a linked list class that stores a linked collection of entities. I've
I need a Global variable/class that stores some basic information about the currently logged
I have a member profiles application that stores simple information about members of a
I have a session class that needs to store session information in a MySQL
I have a class that stores paths to CSS and Javascript files in arrays.
I have a PHP class that stores a complex multidimensional array, and rather than
I have a class that basically stores files in amazon s3. Here is what
I have a file that stores exam scores for a class of students. I
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a base type which stores information about a question in a question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.