开发者

Type annotations of objects to the correct class in Python/Eclipse

开发者 https://www.devze.com 2023-03-11 05:53 出处:网络
I\'m using Eclipse for developing some Python based application. Very often I pass some class object to methods. So how can I say to the eclipse that this particular variable 开发者_如何转开发is of

I'm using Eclipse for developing some Python based application.

Very often I pass some class object to methods. So how can I say to the eclipse that this particular variable 开发者_如何转开发is of type Class A. For ex.

def foo(var1,var2):
    ''' var1 is of type Class A, and var2 is of type Class B'''
    var1.a = 10
    var2.b = 'xyz'

I can use all these ways, problems is that Eciplse can't give the variable hints for all these member variables for Class A and Class B. I've to go and check the member variables manually. This is very hectic and error prone. Is there any way I can tell eclipse/python that var1 is of type Class A and var2 is of type class B?


There exists no such thing as type casting in Python. There's type coercion that happens in arithmetic expressions, e.g., but that's something different. As I explained here, there's no sensible way for Eclipse to do what you want it do do. Since Python 3 there are type annotations in function definitions, as propopsed in PEP-3107, which may or may not be used in the future by programs like PyDev.

Until then, instead of fighting duck typing, you should embrace its power.


I got the same issue ... as far as I've been searching, you can't do that!

Typing Casting in Python Question

0

精彩评论

暂无评论...
验证码 换一张
取 消