Type hinting list of objects
已回答
I've used phpstorm a fair bit and know that you can specify a variable's type by doing @var .....
In pycharm the only way I've found to do that is to assert the variable
for item in self.get_contact_lists():
assert isinstance(item, models.ContactList)
....
Which is great and all, but I'm hoping there is someway I could just type hint in the get_contacts_list method what is being returned.
请先登录再写评论。
I should mention that I have been looking online for solutions and so far what I've found and tried do not seem to work as I'm expecting in the latest version.
http://stackoverflow.com/questions/17824280/how-to-specify-that-a-parameter-is-a-list-of-specific-objects-in-python-docstrin
That is one I've tried without any luck, there are others I've tried over the last few months but so far no luck.
Does this help you? https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
You can specify the return type from the method using
The function is returning a list of objects, not a single object. :rtype: doesn't work for objects within the list itself..
Hi Ian. Sorry for the late reply.
Actually, there are multiple ways to type hint your code in PyCharm (especially, regarding function signatures).
First of all, you can use docstrings, as Marcel suggested earlier. You can write it in whatever docstring format you like. For instance, in reStructuredText
or in Google Python Style (Napoleon) docstrings flavor
The exact syntax of type annotations in docstring that we support is described in details in our python-skeletons project.
Meanwhile, the standard way of adding optional type annotations has emerged in Python community with appearance of PEP 484 and typing module. Unlike, syntax of python-skeletons these annotations are recognized not only by PyCharm, but also in other tools like mypy and pytype.
In Python 3 you can use standard syntax for annotating return value of function introduced back in PEP 3107
And in Python 2/3 compatible code you can use the alternative approach with special "#type:" comments:
There are actually a few articles on this subject you may find helpful:
Perfect, thanks that helps a lot.
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
python training in chennai