使用来自“user"的 pyCharm 进行重构到“self.user" [英] Refactor with pyCharm from "user" to "self.user"

查看:44
本文介绍了使用来自“user"的 pyCharm 进行重构到“self.user"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将变量从局部范围移动到对象级别.新代码应该像以前一样使用 self.user 而不是 user :

I want to move a variable from local scope to object level. The new code should use self.user and not user like before:

class Foo(object):
    def test_foo(self):
        user=User()
        ...
        user.do()

新代码应如下所示:

class Foo(object):
    def test_foo(self):
        self.user=User() # I can remove this line by hand
        ...
        self.user.do()

我尝试重构>>重命名从 userself.user 但 pyCharm 说:插入的标识符无效"

I tried to refactor>>rename from user to self.user but pyCharm says: "Inserted identifier is not valid"

如何使用 pyCharm 重构它?

How can I refactor this with pyCharm?

推荐答案

您要做的不是简单的重命名,而是创建一个新的实例属性.因此,您应该使用 Extract > Field 菜单项,而不是使用 Rename.或者,您可以通过 Ctrl+Alt+F 访问此重构(这取决于您选择的快捷方式.我的是 KDE 的默认设置).

What you are trying to do is not a simple rename, but creating a new instance attribute. As such, instead of using Rename you should use Extract > Field menu item. Alternatively you can access this refactoring as Ctrl+Alt+F (this depends on the shortcuts you have chosen. Mine is the default for KDE).

这篇关于使用来自“user"的 pyCharm 进行重构到“self.user"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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