PyCharm template for python class __init__ function
<p>I have a python class with several init variables:</p>

<pre><code>class Foo(object):
    def __init__(self, d1, d2):
        self.d1 = d1
        self.d2 = d2
</code></pre>

<p>Is there a way to create this code automatically in PyCharm, so I don't have to type explicitly:</p>

<pre><code>self.dn = dn
</code></pre>

<p>This pattern happens very often in my code. Is there a better (Pythonic) way to initialize classes?</p>

<p>I have already seen this post ( <a href="http://stackoverflow.com/questions/3652851/what-is-the-best-way-to-do-automatic-attribute-assignment-in-python-and-is-it-a">What is the best way to do automatic attribute assignment in Python, and is it a good idea?</a>), but I don't want to use a decorator as it makes the code less readable.</p>

<p>You can start by making a custom <a href="https://www.jetbrains.com/help/pycharm/2016.2/live-templates.html" rel="nofollow">Live Template</a>. I'm not sure about whether you can auto-generate variable number of arguments this way, but, for two constructor arguments the live template may look like:</p>

<pre><code>class $class_name$:
    def __init__(self, $arg1$, $arg2$):
        self.$arg1$ = $arg1$
        self.$arg2$ = $arg2$
$END$
</code></pre>

<p>This is how I've set it up in PyCharm:</p>

<p><a href="https://i.stack.imgur.com/7aAma.png" rel="nofollow"><img src="https://i.stack.imgur.com/7aAma.png" alt="enter image description here"></a></p>

<hr>

<p>Or, you can change the way you set the instance attributes and generalize it for N arguments, see:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/8187082/how-can-you-set-class-attributes-from-variable-arguments-kwargs-in-python">How can you set class attributes from variable arguments (kwargs) in python</a></li>
</ul>

python class attributes pycharm