PyCharm可以自动生成__eq __()和__hash __()实现吗? [英] Can PyCharm automatically generate __eq__() and __hash__() implementations?

查看:472
本文介绍了PyCharm可以自动生成__eq __()和__hash __()实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PyCharm新手,但是长期使用IntelliJ用户。在IntelliJ中,当您编写类定义时,IDE可以自动生成构造函数, equals()方法,以及 hashCode()基于实例变量的方法。这不仅可以保存输入,还可以防止意外错误,并自动输入一些 equals() hashCode()最佳实践。



我希望PyCharm可以做同样的事情,因为这些产品来自同一家公司。经过大量谷歌搜索和搜索文档,我找不到任何 __ eq __() __ hash __()。当然,Python实例变量没有明确指定,但我希望生成器可以遵循一个约定,例如提供所有 __ init()__ 参数作为潜在的实例变量。至于 __ init __(),我发现了一些会自动将实例变量设置添加到 __ init __()中,但这种方法似乎比单独输入更麻烦,甚至不会将实例变量添加为参数到 __ init __()签名。



我遗漏了文档中的任何内容,或者是否有插件可以这样做吗?



更新:要清楚,我正在寻找能够生成这些方法的实际实现的东西。也就是说,如果我有一个名为 Point 的类,并且PyCharm知道我的类有 x y 实例变量,然后它会自动为 __ eq __()方法生成此:

  def __eq __(self,other):
如果不是isinstance(其他,Point):
return NotImplemented
elif self is other:
返回True
else:
返回self.x == other.x和self.y == other.y

相当于在IntelliJ中轻松完成。

解决方案

你可以创建一个实时模板



在文件 - >设置 - >编辑器 - >实时模板下
查找Python
单击+添加,然后命名我的类,并确保在gui中为Python文件添加上下文。 / p>

模板文本:

  class $ class_name $:
$ class_docstring $

def __init __(self,$ args $):
$ init_docstring $
传递

def __eq __(self,$ other $):
如果不是isinstance($ other $,$ class_name $):
return NotImplemented
elif self is $ other $:
返回True
else:
返回self。$ eq_field $ == $ other $。$ eq_field $

def __hash __(self,):
传递
$ END $

我离开了选项 - >展开部分设置为默认(Tab)
此后,当您键入class时,可以使用Tab auto-complete插入实时模板。您的光标将被弹回到实时模板文本中作为变量包含的任何部分。



更复杂,即列表类型,LiveTemplate似乎不支持变量。例如,实时模板文本或列表扩展中的条件似乎不可用。


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.

解决方案

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.

这篇关于PyCharm可以自动生成__eq __()和__hash __()实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆