Can PyCharm automatically generate __eq__() and __

2019-04-04 17:54发布

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, equals() method, and hashCode() 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 equals() and hashCode() 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 __eq__() or __hash__(). Granted, Python instance variables are not explicitly specified, but I was hoping the generator could follow a convention like offering all __init()__ parameters as potential instance variables. As for __init__(), I found something that will automatically add instance variable settings into the __init__(), but this approach seems more cumbersome than just typing, and it doesn't even add the instance variables as parameters to the __init__() signature.

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 Point, and PyCharm knows my class has x and y instance variables, then it would automatically generate this for the __eq__() method:

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

The equivalent is easily done in IntelliJ.

2条回答
家丑人穷心不美
2楼-- · 2019-04-04 18:15

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$:
    """$class_docstring$"""

    def __init__(self, $args$):
        """$init_docstring$"""
        pass

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

    def __hash__(self, ):
        pass
    $END$

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.

查看更多
叛逆
3楼-- · 2019-04-04 18:28

Select the class and then do one of:

  • Press Alt-Insert
  • Right Click -> Generate
  • Menu -> Code -> Generate
查看更多
登录 后发表回答