自动完成传递了 class 个对象

Autocomplete passed class object

我有两个 类 说 A 和 B。B 在其构造函数中将 A 作为参数。 Class A 有一个函数 foo。现在,当我输入 "a." 时,我希望 Vs 代码自动完成以显示建议 "a.foo()"。目前无法正常工作。我需要输入提示或类似的东西吗?我试过导入 A 但它不起作用。

class A:
    def __init__(self):
    def foo(self):
        print("hello")
class B:
    def __init__(self, a):
        a. <-- this should show the members of A but does not

运行 Mac 和带有 Microsoft 扩展名的 Python 2.7。

试试这个:

class A:
    def __init__(self):
    def foo(self):
        print("hello")
class B(A):
    def __init__(self):
       super().__init__()

现在您应该能够:

classexp = B()
classexp.{some A function}

Python 是一种 dynamically-typed 语言,您的编辑器无法在运行前评估传递给 B 的构造函数的参数 a 的类型以向您显示其属性.

您可以使用类型提示来告诉您期望将 A 对象作为参数 a 传递给 B:

的构造函数

def __init__(self, a: A):

如果没有类型提示,您可以使用 ctrl(or command)+space 查看所有可能的完成。