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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:31:14+00:00 2026-05-26T22:31:14+00:00

Suppose I have a class with a constructor (or other function) that takes a

  • 0

Suppose I have a class with a constructor (or other function) that takes a variable number of arguments and then sets them as class attributes conditionally.

I could set them manually, but it seems that variable parameters are common enough in python that there should be a common idiom for doing this. But I’m not sure how to do this dynamically.

I have an example using eval, but that’s hardly safe. I want to know the proper way to do this — maybe with lambda?

class Foo:
    def setAllManually(self, a=None, b=None, c=None):
        if a!=None: 
            self.a = a
        if b!=None:
            self.b = b
        if c!=None:
            self.c = c
    def setAllWithEval(self, **kwargs):
        for key in **kwargs:
            if kwargs[param] != None
                eval("self." + key + "=" + kwargs[param])
  • 1 1 Answer
  • 3 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-26T22:31:14+00:00Added an answer on May 26, 2026 at 10:31 pm

    You could update the __dict__ attribute (which represents the instance attributes in the form of a dictionary) with the keyword arguments:

    class Bar(object):
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
    

    then you can:

    >>> bar = Bar(a=1, b=2)
    >>> bar.a
    1
    

    and with something like:

    allowed_keys = {'a', 'b', 'c'}
    self.__dict__.update((k, v) for k, v in kwargs.items() if k in allowed_keys)
    

    you could filter the keys beforehand (use iteritems instead of items if you’re still using Python 2.x).

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

Sidebar

Related Questions

Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have a class with some attributes. How is it best (in the
Suppose I have a class 'Application'. In order to be initialised it takes certain
Suppose we have this code: class My_controller extends CI_Controller { private $model_name; function __construct($model_name)
Let's suppose that I have the following class which tries to be immutable public
Suppose we have a Class A, and B. inside B's constructor, not only A
Suppose you have a class Dog , that has public class Dog { private
Suppose I need to have a class which wraps a priority queue of other
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.