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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:23:31+00:00 2026-05-19T17:23:31+00:00

I noticed I often write the following: class X: def __init__(self, var1, var2, var3):

  • 0

I noticed I often write the following:

class X:
  def __init__(self, var1, var2, var3):
    self.var1 = var1
    self.var2 = var2
    self.var3 = var3
    # more code here

Is it a good idea to make a template that I can reuse instead of doing this every time? If so, how should I do that?

  • 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-05-19T17:23:32+00:00Added an answer on May 19, 2026 at 5:23 pm

    I wouldn’t suggest using such templates in production code, because

    Explicit is better than implicit.
    

    For throw-away prototypes it might be acceptable. Here is an example from the python recipes:

    • http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/

    It defines a decorator with can be attached to __init__:

    def injectArguments(inFunction):
        """
        This function allows to reduce code for initialization 
        of parameters of a method through the @-notation
        You need to call this function before the method in this way: 
        @injectArguments
        """
        def outFunction(*args, **kwargs):
            _self = args[0]
            _self.__dict__.update(kwargs)
            # Get all of argument's names of the inFunction
            _total_names = \
                inFunction.func_code.co_varnames[1:inFunction.func_code.co_argcount]
            # Get all of the values
            _values = args[1:]
            # Get only the names that don't belong to kwargs
            _names = [n for n in _total_names if not kwargs.has_key(n)]
    
            # Match names with values and update __dict__
            d={}
            for n, v in zip(_names,_values):
                d[n] = v
            _self.__dict__.update(d)
            inFunction(*args,**kwargs)
    
        return outFunction
    

    A test:

    class Test:
        @injectArguments
        def __init__(self, name, surname):
            pass
    
    if __name__=='__main__':
        t = Test('mickey', surname='mouse')
        print t.name, t.surname
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed some code of a colleague today that initialized class variables in the
I noticed that a lot of tutorial instructions often have this code: $sql=SELECT *
I noticed some posts here on string matching, which reminded me of an old
I noticed a good while back that Wikipedia links to a Javascript implementation of
I've seen it happen reasonably often: I write an application in Delphi and when
I often use links with href='#' when calling ajax resources. I noticed that IE
I often write classes with a DLL export/import specification, but this seems to confuse
In many scripts that I write, I often construct programs in a functional style.
Like lots of you guys on SO, I often write in several languages. And
I am increasingly aware that my code in any single file can often span

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.