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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:34:04+00:00 2026-05-23T16:34:04+00:00

I have a class who’s _ init _ function requires quite a few keyword

  • 0

I have a class who’s _init_ function requires quite a few keyword arguments. I’d like to be able to basically rewrite this bit of code so that it’s syntactically cleaner (less hard coding). Preferably I’d like to be able to get it so that simply adding a keyword argument to _init_ would change all the attributes/arguments in the null function respectively.

class Class :

    def __init__ (self, kw0=0, kw1=1, kw2=2) :

        ''' The keyword arguments as strings. '''
        self.Keys = ['kw0', 'kw1', 'kw2']

        ''' Their values. '''
        self.Values = [kw0, kw1, kw2]

        ''' A dictionary made from the keys and values. '''
        self.Dict = self.make_dict()

        ''' As individual attributes, '''
        self.KW0, self.KW1, self.KW2 = self.Values

    def make_dict (self) :
        ''' Makes a dictionary '''

        keys   = self.Keys
        values = self.Values

        _dict = {}
        for i in xrange(len(keys)) :
            key   = keys[i]
            value = values[i]

            _dict[key] = value

        return _dict

    def null (self, kw0=None, kw1=None, kw2=None) :
        ''' The same keyword arguments as **__init__** but they all default
            to **None**. '''

        pass

c = Class()
print c.Keys
print c.Values
print c.Dict
print c.KW0
print c.KW1
print c.KW2
  • 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-23T16:34:05+00:00Added an answer on May 23, 2026 at 4:34 pm

    Why not accept any keyword arguments. You can use a class attribute for allowable keyword names and their default values.

    class Class(object):
    
         _defaults = dict(kw0=42, kw1=None, kw2=True, kw3="Ni!")
    
         def __init__(self, **kwargs):
    
            # Raise exception if any non-supported keywords supplied
            if set(kwargs.keys()) - set(self._defaults.keys()):
                raise KeyError("unsupported keyword argument")
    
            # Update our instance with defaults, then keyword args
            self.__dict__.update(self._defaults)
            self.__dict__.update(kwargs)
    

    If you want the same functionality in more than one method (e.g. __init__() and null()) then just break the argument handling code out into its own method and call it from both places.

    One downside is that help() and other Python documentation tools won’t show the allowable keyword arguments, since they aren’t in your method signature.

    As an aside, I’m not quite sure why you’re storing keys and values separately. Just store them as a dictionary, then get the keys or values when you want them using the dictionary’s .keys() or .values() method.

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

Sidebar

Related Questions

Let's say I have an object who's class definition looks like: class Command: foo
I have a class and subclasses who extends that. Like this: @Table @Entity class
I have a bunch of class who implement a common interface : Command. And
Here is my problem, I have a class which have a object who throw
I have class with a member function that takes a default argument. struct Class
So say I have a class like this: class A { public: A( SomeHugeClass*
Let's say I have class A who inherits from class B and C (multiple
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>
I have a class defined like so: public class Test { static <T> List<Class<T>>
I have a problem with C#, I have a Class with a function for

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.