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

  • Home
  • SEARCH
  • 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 9235883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:09:27+00:00 2026-06-18T07:09:27+00:00

I have begun to take my first steps in learning python and am trying

  • 0

I have begun to take my first steps in learning python and am trying to start using classes.

I have this simplified version so far:

class ThingsInTheFridge(object):
  def __init__(self):
    self.milk = False
    self.yogurt = False
    self.bread = True
    self.umbrella = True

There will be about 30 things in my class and each is assigned True or False (I have shortened the list for this question).

What is the most efficient way to assign them?

I have thought about this but it doesn’t seem to improve things especially when the list increases in size:

self.milk, self.yogurt = False, False

EDIT:
I perhaps should have mentioned there are other items in the class to (I omitted them because I didn’t think it would matter):

class ThingsInTheFridge(object):
  def __init__(self):
    self.milk = False
    self.yogurt = False
    self.bread = True
    self.umbrella = True
    self.bulb = "maker name"
    self.shoes = 2
  • 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-06-18T07:09:28+00:00Added an answer on June 18, 2026 at 7:09 am

    If you really care about runtime efficiency, you’re almost certainly looking in the wrong place. If initializing your members takes too long (which is very unlikely… but if you profile and find this is the hotspot…), the right answer is to use __slots__, not to change the way you do the initialization.

    If you care about programmer efficiency (readability, and to a lesser extent writability), you can always do this:

    self.milk = self.yogurt = False
    

    If you want to do things more dynamically, you could do something like this with setattr, but I wouldn’t recommend it:

    class ThingsInTheFridge(object):
        false_things = ('bread', 'umbrella')
        true_things = ('milk', 'yogurt')
        def __init__(self):
            for thing in ThingsInTheFridge.false_things:
                setattr(self, thing, False)
            for thing in ThingsInTheFridge.true_things:
                setattr(self, thing, True)
    

    The reason I wouldn’t recommend it is that, if you need to do things dynamically at this level, you also probably need to be dynamic at the access level, which means you really should be using a dict or set, as explained by Lattyware.

    You could also go overboard and build yourself a prototype-style OO system on top of the class-style system, or create a metaclass that initializes instances based on class information, etc., but, as the start of this sentence indicates, that would be going overboard.

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

Sidebar

Related Questions

I have begun some serious reading on this framework and i am seeing where
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
I have just begun working on a project which uses Mercurial as a version
I've recently begun learning C# but have encountered an annoying problem. Every variable I
I have begun putting together an application using phonegap. I have completed the core
I have begun creating a wpf mvvm app. It seems a vital ingredient to
Per a great answer from another question I have begun mounting global resources (css/js/images)
I have just begun working with the Android SDK and I am having problems
I have just begun to use Visual Studio Team System 2008 Database Edition. I'm
I have recently begun delving into the world that is database programming with C++

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.