简体   繁体   中英

How to declare object type in python

I want the IDE (pycharm) to know the type of an varliable

class phone:

    def __init__(self,):
        self.device = None
    def connect(self,):
        self.device = samsung_object()

Now the IDE don't know the functions/vars used for device. I want that in init time it will be a samsung object type.

Converting comments into code for you :)

This will works and PyCharm knows the type too..

class Samsung:
    def foo(self): pass

class Phone:
    def __init__(self,):
        self.device = Samsung()
    def connect(self,):
        self.device.foo() # pycharm suggest foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM