【问题标题】:Python, Pycharm get code completion from dynamic variablesPython、Pycharm 从动态变量中获取代码补全
【发布时间】:2022-01-20 22:18:01
【问题描述】:
class FooBar:
    attribute_map = {
        "foo": "bar",
        "bar": "foo"
    }

    def __init__(self):
        setattr(self, "foo", "bar")
        setattr(self, "bar", "foo")


foo_bar = FooBar()
print(foo_bar.foo)
print(foo_bar.bar)

我的代码看起来像这样。我希望 pycharm 使用属性映射来显示动态变量 foo 和 bar 的自动完成。这可能吗?

【问题讨论】:

    标签: python pycharm


    【解决方案1】:

    不,这是不可能的。 PyCharm 的自动补全不支持动态设置属性。

    您可以像这样进行类型注释:

    class FooBar:
        foo: str
        bar: str
    
        attribute_map = {
            "foo": "bar",
            "bar": "foo"
        }
    
        def __init__(self):
            setattr(self, "foo", "bar")
            setattr(self, "bar", "foo")
    
    
    foo_bar = FooBar()
    print(foo_bar.foo)
    print(foo_bar.bar)
    

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 2013-06-30
      • 2013-01-14
      • 2018-01-18
      • 2022-01-21
      • 2015-12-04
      • 1970-01-01
      • 2013-07-03
      相关资源
      最近更新 更多