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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:11:34+00:00 2026-06-14T13:11:34+00:00

Possible Duplicate: Assign function arguments to `self` Often I have constructors that look like

  • 0

Possible Duplicate:
Assign function arguments to `self`

Often I have constructors that look like the following:

class Foo(object):
  def __init__(self,a,b,c):
    self.a = a
    self.b = b
    self.c = c

I was wondering if there was a convenient way to encode constructors like this. Perhaps something that looks like:

class Foo(object):
  SOME_MACRO_LIKE_THINGY_THAT_SPECIFIES_THE_CONSTRUCTOR(a,b,c)

that would behave exactly like the original code above.

What bugs me with the original version is that I have to write each instance variable of Foo three times (once as an argument, once as self.a and once again as the value to assign to self.a).

I suppose it’s not that big a deal, but I feel like the code would look neater with less repetition.

What is the Pythonic way to handle this situation?

  • 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-14T13:11:36+00:00Added an answer on June 14, 2026 at 1:11 pm

    It’s weird, but you could use __slots__ to achieve this:

    class Base(object):
        def __init__(self, *args, **kw):
            for k,v in zip(self.__slots__, args):
                setattr(self, k, v)
    
            for k,v in kw.items():
                setattr(self, k, v)
    
    class ChildA(Base):
        __slots__ = 'a', 'b', 'c'
    
    class ChildB(Base):
        __slots__ = 'x', 'y', 'z'
    

    You could also use the following technique to save yourself some typing when initializing classes with lots of arguments:

    def autoargs(l):
        self = l.pop('self')
        for k,v in l.items():
            setattr(self, k, v)
    
    class Base(object):
        def __init__(self, a, b, c):
            autoargs(locals())
    

    I hope I’m understanding your question correctly.

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

Sidebar

Related Questions

Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: How do I assign by “reference” to a class field in c#?
Possible Duplicate: Conditional operator in Python? Is there a c-like operator in python that
Possible Duplicate: jquery reading nested json I would really like to have a hard
Possible Duplicate: Self-references in object literal declarations I have an object literal which is
Possible Duplicate: Adding css class through aspx code behind Im trying to assign a
Possible Duplicate: How to create multiline strings is it a way to assign following
Possible Duplicate: Get query string values in JavaScript I have a URL like below
Possible Duplicate: How do I assign by “reference” to a class field in c#?
Possible Duplicate: Assign a value to class variable is assiging it for all instances

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.