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 that I'm often in the need of a container class. For example
I often find I want to write code something like this in C#, but
I noticed that a lot of tutorial instructions often have this code: $sql=SELECT *
I've noticed that the domain contoso.com is often used in documentation when a sample
Noticed this today when a patch was submitted with the following line: lblCompletionTime.Text =
I noticed some posts here on string matching, which reminded me of an old
Often I write functions whose operation depends depends on the type of some argument.
I prefer to develop with E_NOTICES turned on, but I often have to write
I have noticed that with Delphi 2009, I often get strange errors when compiling,
Using require.js I noticed that often the dependencies are cached by the browser and

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.