Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define class for input variable in VS Code

I’m learning python, suppose I have a class and want to use it as a input variable for other defs:

class person:
    def get_name(self):
        return self.name
    def set_name(self):
        self.name = name

def dummy(p):
    print(p.get_name())

How to tell the IDE know p is an instance of the class person and suggest the defs of the person like Java.
Thanks you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Consider using a type hint. These are classes you can suggest a certain value will be. Consider this

def say_hello(name: str, age: int) -> None:
    message: str = f'Hello, {name}! You are {age} years old'
    print(message)

Here, we say a function say_hello will take two args: name, which should be an instance of the str class, and age, which should be an instance of the int class. The function should return None, and it defines a local variable message which should also be a string.

Notice how each time I said "should". Python is a dynamically-typed language, so none of these will cause an error if their values aren’t instances of the specified class. These are just a peak at what the variables should be for the developer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading