0

in order adapt some code I find myself looking for the appropriate type in my python code. Is there a way to avoid this or do this faster?

Consider the following code for example:

def my_function(my_list)
   for my_entry in [entry for entry in my list if ...]

my_entry in this case is a namedTuple, but I have to look where my_function is used and look up the "invocation"-chain in order to know that my_list is a list of namedTuples. Is there a better solution? I am used to statically typed languages.

Just to make it clear, I am not necessary looking for type hints, but for good solution.

David
  • 2,926
  • 1
  • 27
  • 61
  • 2
    For one, better variable naming helps. Secondly, you can use [typing](https://docs.python.org/3/library/typing.html) for type hints – shriakhilc Jun 04 '19 at 10:58
  • 1
    Type hinting helps make python look more like static language and a good Autocompleter (e.g. jedi) would be sufficient for the 80% of cases. However, just like in any dynamic language it's often necessary that you should just put a breakpoint there (e.g. IPython, pudb) and introspect the live objects. – Lie Ryan Jun 04 '19 at 11:27
  • 1
    The simplest solution is to document the parameters when writing the code... FWIW a good naming scheme can often be enough as documentation. – bruno desthuilliers Jun 04 '19 at 11:43

0 Answers0