TransWikia.com

Python Hints for function inputs in Jupyter

Stack Overflow Asked by Ricardo on September 12, 2020

Is it possible to add some sort of decorator or alias to get hints on the possible inputs a variable can get in a function?

In the following example function input_var can take any values, but can I hint to use the values ‘year’,’month’ or ‘day’?

def some_function(input_var):
    """ This function does something cool, use input_var as bla, blah or blahh """
    if input_var=='year':
        print("Selected 1")
    if input_var=='month':
        print("Selected 2")
    if input_var=='day':
        print("Selected 3")

so far I add the "hint" to the docstring, but I’m want something like this:

jupyter screenshot

where it could show input_var=’year/month/day’ or something like that.

Using Jupyter notebooks, with python 3

Thanks

One Answer

  • look at function annotations.
  • Example:
def foo(x: 'year/month/day'):
  return "abc"
  • now if you type x and press tab for suggestions it will show x : 'year/month/day'.

Correct answer by Poojan on September 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP