相关疑难解决方法(0)

如何使用静态检查来确保对象具有特定的方法/属性?

Is there I way that I can annotate a function to ensure that an object being passed into the function has a certain method or attribute, but where I don't care about its actual type?

Pycharm internally uses a syntax that looks like {b} to indicate what methods/attributes it's inferred are required for the object, but that doesn't appear to be valid Python syntax:

def func(a: {b}):  # Error
    a.b = 1
Run Code Online (Sandbox Code Playgroud)

Is there a way to get the type …

python duck-typing type-hinting python-3.x python-typing

4
推荐指数
1
解决办法
723
查看次数