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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:14:59+00:00 2026-06-10T15:14:59+00:00

Constructors in Python often look like this: class SomeClass: def __init__(self, a, b =

  • 0

Constructors in Python often look like this:

class SomeClass:
    def __init__(self, a, b = None, c = defC):
        self.a = a
        self.b = b or []
        self.c = c

Is there a shortcut for this, e.g. to simply define __init__(self,**kwargs) and use the keys as properties of self?

  • 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-10T15:15:01+00:00Added an answer on June 10, 2026 at 3:15 pm

    One problem with

    self.__dict__.update(locals())
    

    is that it includes self, so you get self.self. It would be better to filter self out of locals()

    eg.

    vars(self).update((k,v) for k,v in vars().items() if k != 'self')
    

    You can defend against accidentally overwriting methods with this variation

    vars(self).update((k,v) for k,v in vars().items()
                       if k != 'self' and k not in vars(self))
    

    If you don’t want it to fail silently, you could also check beforehand like this

    if any(k in vars(self) for k in vars()):
        raise blahblah
    vars(self).update((k,v) for k,v in vars().items() if k != 'self')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
I am writting a Python class with this constructor: #constuctor def __init__(self, initPt_=[1,1],fun_=Optim_tests.peaks,NITER_=30,alpha_=0.7,NMAX_=5000,FTOL_=10**(-10)): self.initPt
I wanted to create my own Python exception class, like this: class MyException(BaseException): def
I have two constructors in my class: def __init__(self): self(8) def __init__(self, size): self.buffer
I find that my constructors are starting to look like this: public MyClass(Container con,
class A(object): def __init__(self, a, b, c): #super(A, self).__init__() super(self.__class__, self).__init__() class B(A): def
I have the following base class: class NeuralNetworkBase: def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs): self.inputLayer
There is this code: #include <iostream> class SomeClass { int someInteger; float someFloat; public:
I'm finding myself writing a lot of classes with constructors like this: class MyClass(object):
In base class constructors I always see a parameterless constructor, like so: public abstract

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.