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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:06:58+00:00 2026-06-05T16:06:58+00:00

I have this code: def __init__(self, a, b, c, d…): self.a = a self.b

  • 0

I have this code:

def __init__(self, a, b, c, d...):
   self.a = a
   self.b = b
   etc

I’m thinking of replacing it with:

def __init__(self, a, b, c, d...):
   args=locals()
   for key in args:
     self.__dict__[key] = args[key]

Is this a bad idea? Are there any better ways to do this?

  • 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-05T16:07:00+00:00Added an answer on June 5, 2026 at 4:07 pm

    Building on @ThiefMaster’s comment about **kwargs:

    If you are taking in 20 arguments, it might make more sense to require your users to send arguments via keyword instead of position: with 20 arguments, there is a decent chance that someone using your code will get the arguments in the wrong order.

    Consider only accepting kwargs while having a predefined list of keys you want to accept and raising a ValueError if you don’t receive them. So you could use **kwargs and then check that everything is there. E.g.

    INITIAL_ARGS = set(['a','b','c','d','e'...])
    
    def __init__(self, **kwargs):
        if not INITIAL_ARGS.issubset(set(kwargs.iterkeys())):
            raise ValueError("Class <myclass> requires 20 keyword arguments"
                              "only given %d" % len(kwargs))
        self.__dict__.update(kwargs)
    

    Not sure whether this is more or less Pythonic than your original, but it seems like it would save a ton of time later on when trying to figure out why someone using your code might be getting strange errors.

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

Sidebar

Related Questions

I have this code: import wx class Plugin(wx.Panel): def __init__(self, parent, *args, **kwargs): panel
Let's say I have this code: class class1(object): def __init__(self): #don't worry about this
Hello i have this code class Test(object): def start_conn(self): pass def __init__(self): self.conn =
I have this (example) code: init() class A: foo = bar() def __init__(self): print(A.foo)
I have this code: class API(object): def __init__(self): self.baseuri = http://api.xxx.xxx self.cj = cookielib.CookieJar()
Suppose I have this code: class Num: def __init__(self,num): self.n = num def getn(self):
I have written this code: class component(object): def __init__(self, name = None, height =
I have this code: >>> class G: ... def __init__(self): ... self.x = 20
this code: import wx app = None class Plugin(wx.Panel): def __init__(self, parent, *args, **kwargs):
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +

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.