2

Is there any way to list possible values of an argument as I call function inside PyCharm editor or console ?

Here's a basic function example:

def func(x='foo'):

    if x=='foo':
       print('foo')
    if x=='bar':
       print('bar')

When I type func(x=, I want to get suggestions for parameter or at least the default value.


EDIT 1: How can I make the autocomplete print usage of particular function?


EDIT 2: how can I replicate this behavior from Keras LSTM ?

Keras example function

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
Alex Deft
  • 2,531
  • 1
  • 19
  • 34

1 Answers1

1

Modified example you provided:

def func(x='foo'):
    """My awesome function.

    This function receives parameter `x` which 
    can be set to 'foo' or 'bar'.
    """

    if x=='foo':
       print('foo')
    if x=='bar':
       print('bar')

To get parameter info, use Ctrl+P shortcut between the parenthesis when calling a function. It will display list of arguments, along with the default ones for specific argument (both default values are set in your function and in Keras function).

However, to display function help(docstring of a function), use Ctrl+Q shortcut while anywhere inside function name when invoking it.

BONUS:

Python supports Type Annotations using a typing module. You can create enumeration and set it as parameter type as following:

import enum


class State(enum.Enum): # Possible values
    STANDING = 1
    SITTING = 2
    LAYING = 3


def get_person_state(state: State):
    if isinstance(state, State):  # Restrict possible values passed to state.
        print("Valid state:{}".format(state))
    else:
        print("Invalid state passed")

get_person_state(State.SITTING)
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • Thanks for the bonus. But, can you please tell me what's the point of the enum module? I'm asking this question because I have achieved what I want without it. Just make `class State' without inheriting from enum and you're completely fine. So, why did they make it? – Alex Deft Apr 12 '20 at 03:59
  • 1
    There are perfect answers already for that question at https://stackoverflow.com/questions/37601644/python-whats-the-enum-type-good-for and https://stackoverflow.com/questions/22586895/python-enum-when-and-where-to-use . The first answer in first quesion gives pretty much all explanation on how are enums different and when to use them. I'll try to update my answer to be more understandable. – Dinko Pehar Apr 12 '20 at 09:07