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 6936027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:10:50+00:00 2026-05-27T12:10:50+00:00

THanks for looking into my post. I guess I have a probably a pretty

  • 0

THanks for looking into my post. I guess I have a probably a pretty simple doubt by this community standards.. but pardon me if it is too simple. I am trying to learn python.
So anyways.. I have an object which can have five attributes.
So lets say when i am initializing the class its like this

class Foo:
    def __init__ (self, argA = None, argB = None, argC = None, argD = None....)

and somewhere I have this method called get features

    def getFeatures(self):
     x = [self._argA, self._argB, self._argc... and so on]

so basically I am trying to put all these features into a list.
But here is the problem.
During initialization, as you can see all the parameters are optional..
but when I do getFeatures, I only want those features which are initialzed
Now a naive way is to write a big if else statement but as Scott Myers wrote in is book.. anytime you see yourself writign a code ” if object is of type A do something and if object is of type B do something else… then slap your self..”
What is a good way to getFeatures whcih contains only the elements which are initialized.
Thanks

  • 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-27T12:10:50+00:00Added an answer on May 27, 2026 at 12:10 pm

    Keep a dictionary of feature name to boolean enabled/disabled. That’s much better than having all these variables floating around.

    def __init__(self, **kwargs):
        self.features = kwargs
    
    def getFeatures(self):
        return {feature for feature, isEnabled in self.features.items() if isEnabled}
    

    Example:

    >>> class Foo(object):
    ...     def __init__(self, **kwargs):
    ...             self.features = kwargs
    ...     
    ...     def getFeatures(self):
    ...             return {feature for feature, isEnabled in self.features.items() if isEnabled}
    ... 
    >>> x = Foo(red=True, yellow=False, green=True)
    >>> x.getFeatures()
    set(['green', 'red'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for looking on this problem. I have a page that is totally valid
I went through this post. Detect Click into Iframe using JavaScript But still, I
Hey. You may have recently seen a post by me looking for help, but
Thanks for looking, all helpful answers are voted up. This is my markup. I'm
Thanks for looking at this question. I wanted to know how can I use
and thanks for looking at this question. I am porting a Linux (2.6.37) driver
thank you for looking i got this example from my book but i cant
My limited SQL knowledge prompted this post! I have a stored procedure that runs
First Post. I'm a mobile game developer looking into using the Java version of
I have a question. Recently I was looking into ways to implement hardware rendering

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.