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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:54:51+00:00 2026-06-17T20:54:51+00:00

This question is specifically in the context of Python classes here, but could be

  • 0

This question is specifically in the context of Python classes here, but could be more generalized. I am creating a class m that will be initialized with four variables, which I would then like to assign to the instantiation of the class. Right now, this looks like this:

class mm(object):
  def __init__(self, a, b, c, r):
    self.a = a
    self.b = b
    self.c = c
    self.r = r
  # etc.

However, I would like to be able to assign the variables to self in one line, something like the pseudocode:

def __init__(self, a, b, c, r):
  self.varname = var for var in [a, b, c, r]

It would be even better if this could be generalized to an arbitrary number of variables, so that you could get something like:

def __init__(self, **kwargs):
  assign(self, varname, value) for varname, value in kwargs.iteritems()

However, as far as I know, it’s not really possible to loop through statements like that. Does anyone know a way 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-17T20:54:52+00:00Added an answer on June 17, 2026 at 8:54 pm

    You can use setattr:

    class mm(object):
        def __init__(self, **kwargs):
            for varname, value in kwargs.iteritems():
                setattr(self, varname, value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is specifically about PHP, but I'm guessing it might be applicable to
I have seen there is no question that specifically reply to this combination of
Context: This is specifically a Windows 8 Metro application question based on their HTML/JS
I'm looking at the kauth tech note , but this isn't about that specifically.
My question is analogous to this one but in the context of importing R
This question is a bit specific, but here goes: I'd like to use ZZipLib
I am working in Python, but this is a general design question so general
I asked this question in a more general design context before. Now, I'd like
I think this question is more a javascript question than a backbone question, but
Unlike the other posts about the task delete all tables, this question is specifically

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.