Ricardo
Ricardo

Reputation: 374

Python Hints for function inputs in Jupyter

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

Upvotes: 2

Views: 2476

Answers (1)

Poojan
Poojan

Reputation: 3519

  • 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'.

Upvotes: 3

Related Questions