How to tell PyCharm that a function parameter is an instance of something ?
The question is simple but you must excuse me if I start with a long preamble. Let's say I have a class and a function that accepts an instance of this class as a parameter. For example:
class Person(object):
""" This class defines a person """
def __init__(self, P_cSecondName = "", P_cFirstName = ""):
self.firstname = P_cFirstName
self.secondname = P_cSecondName
def greet(P_objPerson):
""" This function greets a person """
print("Hi")
clark = Person("Kent", "Clark")
greet(clark)
>>> Hi
Now I want to use the properties of the class "Person" to greet this guy, so inside the function instead of:
print("Hi")
I'm writing:
print("Hi {0} !".format(P_objPerson.firstname))
>>> Hi Clark !
It's simple. What is my problem then? If I don't remember the properties and methods of the "Person" class, how do I ask PyCharm to give me a list of them? Basically I would like to write in the source code the word "P_objPersona", press the dot key and see a pop-up window with the lists of each property and each method available inside the class "Person".
For example in WingIDE I can write:
def greet (P_objPerson):
isinstance (P_objPerson, Person)
...
WingIDE automatically recognize that when I wrote P_objPerson I'm referring to an instance of the class "Person", but on PyCharm I tried to do the same and it doesn't work.
So, finally, my question is: On PyCharm how do you do that?
Many thanks and best regards
Please sign in to leave a comment.
Hi.
You defentely can use assert isinstance(obj, SomeClass).
This works.
---
wbr, Serge Travin.
I'm unable to get it working.
This is what I have tried to write:
def greet(P_objPerson):
""" This function greets a person """
isinstance(P_objPerson, Person)
but when I try to write "P_objPerson." I'm not getting any suggestion. When I try to press CTRL+SPACE a tiny pop-up window appears with the phrase "No suggestions" written in it.
def greet(P_objPerson):
""" This function greets a person """
assert isinstance(P_objPerson, Person)
Aw, sorry I didn't understand the first time you wrote it.
Thank you very much, it's working wonderfully :-)