PyCharm template for python class __init__ function

You can start by making a custom Live Template. 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:

class $class_name$:
    def __init__(self, $arg1$, $arg2$):
        self.$arg1$ = $arg1$
        self.$arg2$ = $arg2$
$END$

This is how I've set it up in PyCharm:

enter image description here


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


I learned of another way to solve this.

  • Move cursor to argument
  • Press alt + enter
  • Select Add field '[arg]' to class for single arg -or-
  • Press right arrow key to open sub menu
  • Select Fix all 'unused local' problems to add all args

enter image description here