sparc_spread sparc_spread - 2 months ago 49x
Python Question

Can PyCharm automatically generate __init__(), __eq__() and __hash__() implementations?

I am a PyCharm newbie but a long-time IntelliJ user. In IntelliJ, when you are writing a class definition, the IDE can automatically generate a constructor,

method, and
method based on the instance variables. This is good not just for saving typing, but for preventing inadvertent errors and for automatically throwing in some
best practices.

I was hoping PyCharm could do the same, given that the products are from the same company. After much Googling and scouring of the documentation, I couldn't find anything for
. Granted, Python instance variables are not explicitly specified, but I was hoping the generator could follow a convention like offering all
parameters as potential instance variables. As for
, I found something that will automatically add instance variable settings into the
, but this approach seems more cumbersome than just typing, and it doesn't even add the instance variables as parameters to the

Am I missing anything in the documentation, or perhaps there is a plugin that can do this?

Update: to be clear, I am looking for something that generates the actual implementations of these methods. That is, if I had a class called
, and PyCharm knows my class has
instance variables, then it would automatically generate this for the

def __eq__(self, other):
if not isinstance(other, Point):
return NotImplemented
elif self is other:
return True
return self.x == other.x and self.y == other.y

The equivalent is easily done in IntelliJ.


You can create a Live Template

Under File->Settings->Editor->Live Templates Look for Python Click + to add, I then name mine "class" and make sure to add a context in the gui for Python files.

The Template Text :

class $class_name$:

    def __init__(self, $args$):

    def __eq__(self, $other$):
        if not isinstance($other$, $class_name$):
            return NotImplemented
        elif self is $other$:
            return True
            return self.$eq_field$ == $other$.$eq_field$

    def __hash__(self, ):

I left my "Options" -> "Expand with" section set to "Default (Tab)" After this point, when you type "class" you can use Tab auto-complete to insert the live template. Your cursor will be bounced into any section included as a variable in the Live Template Text.

More complicated, i.e. list type, variables don't appear to be supported by LiveTemplate. For instance, conditionals in the live template text, or list expansions, don't appear to be available.