方法参数自动完成课程实例

发布于 2025-01-29 01:07:38 字数 900 浏览 2 评论 0 原文

我正在尝试为课堂实例自动完成方法参数名称,但无法弄清楚。举此示例类:

class Abc:
    def meth(param):
        pass

如果我键入 abc.meth(p ,请按 tab 我看到 param = 的预期完成:

但是,如果我尝试使用类的实例 abc()。甲基结果:

我在Jupyterlab和VS代码中都看到了相同的行为,以及这对于Scikit Learn等其他软件包的实例有效的事实,我认为我缺少某些东西。

<

如何获得与 abc()类的实例相似的类似于 LinearRegression 类的实例?

I am trying to get auto completion of method parameter names for class instances but can't quite figure it out. Take this example class:

class Abc:
    def meth(param):
        pass

If I type Abc.meth(p and press Tab I see the expected completion of param=:

enter image description here

However, if I try to do the same with an instance of the class Abc().meth(p, I don't see param in the completion results:

enter image description here

I see the same behavior in both JupyterLab and VS Code, and the fact that this works for instances of other packages such as scikit learn, I think there is something I am missing.

enter image description here

How can I get method parameter completion for an instance of my Abc() class similar to the LinearRegression class above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2025-02-05 01:07:38

基于Jupyterlab版本3.2.9上的实验,这似乎是因为自动完成试图考虑 self cls 之类的隐式参数。包括 self 参数应主要解决您的问题。

class Abc:
    def meth(arg1, arg2, arg3):
        pass

这些是我为上述类提出的完成选项:

decorator abc.meth abc()。甲基
none arg1,arg2,arg3 arg2,arg3
@staticmethod arg1,arg2,arg2,arg3 arg2,arg3 arg2,arg3
@classmethod arg2,arg2,arg3 arg3 arg3 arg3 arg3

标准行为很好,但是 abc()的结果是两个装饰器的错误都是错误的。如 staticmethod ) and classMethod

方法可以在类(例如cf())或实例上调用(例如c()。f()。f())

因此使用装饰器时,这两个列都应该是相同的,但是它总是从中省略一个参数 abc()。Meth,大概是 self 。即使 cls 正确处理 abc.meth 在使用 @ClassMethod 时,它最终在 abc()中省略了两个参数。 .METH 而是。


Visual Studio代码1.67.1的测试给出了所有情况的正确自动完成选项。因此,由于 param 取而代之的是 self ,并且没有其他参数。

Based on experimentation on JupyterLab Version 3.2.9, this seems to be because the autocomplete tries to account for implicit parameters like self and cls. Including a self parameter should mostly fix your issues.

class Abc:
    def meth(arg1, arg2, arg3):
        pass

These are the completion options I was presented for the class above:

Decorator Abc.meth Abc().meth
None arg1, arg2, arg3 arg2, arg3
@staticmethod arg1, arg2, arg3 arg2, arg3
@classmethod arg2, arg3 arg3

The standard behavior is fine, but the results for Abc().meth are wrong with both decorators. As mentioned in the docs for staticmethod and classmethod:

method can be called either on the class (such as C.f()) or on an instance (such as C().f())

So both columns should have been the same when decorators are used, but it always omits one parameter from Abc().meth, presumably for self. And even though cls is correctly handled for Abc.meth when using @classmethod, it ends up omitting two parameters in Abc().meth instead.


Testing with Visual Studio Code 1.67.1 gave the correct autocomplete options for all cases. So the missing suggestion you experienced there is expected behavior since param takes the place of self and there are no other parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文