Pergunta

I'm working on some basic Python code but I've got a problem I've never been able to solve. Indeed, I would like to change the helptip of a function and I can totally do it.

First of all, here is my code (for example) :

def main(arg1,arg2,arg3):
    #some blabla
    return 1

The thing is, if I start calling this function (in IDLE for example), I have an helptip appearing which just get the same syntax as my function is defined : main(arg1,arg2,arg3).

I would prefer to have something like main(Responsible name, Responsible nickname, Responsible telephone), which represents way more better what each args are. I've already try some docstring implementation but I can only get the two lines together but not remove the first one with the arg1 ...

Can someone tell me if there is a way to get what I want ?

Foi útil?

Solução

Two things:

  1. If you want to have named arguments, that's fine; define your function with them: def main(name, nickname, phone):; and
  2. For docstrings, the format isn't # (comment), it's """ (multiline string).

For example:

def myfunc(name, age, height):
    """Returns a growth-rate for the person based on age and height."""
    return height / age

Now the "tooltip" reads:

(name, age, height)
Returns a growth-rate for the person based on age and height.

If you don't want any named arguments to be shown, you can use *args:

def myfunc(*args):
    """Takes three arguments: name, age and height, returns a growth rate."""
    name, age, height = args
    return height / age

Now the tooltip is:

(...)
Takes three arguments: name, age and height, returns a growth rate.

Outras dicas

well, basically you can't do what you say you want. You need to change the prototype of your function to match what you want to see in the doc, even though that means using very long variable names in your code.

If you take the Zen of python (explicit is better than implicit), you need to choose carefully your variable names, so they are explicit and not too long. So in either cases, your name choice is bad, arg0,arg1,arg2 is awful because it means nothing, and Responsible name... is too long (and I won't even mention that it makes no sense including a space).

And then, it's part of the docstring to give a sentence for each argument to say what that argument is all about, typically:

def afunc(foo, bar):
    """This is a function
    @param foo this is the first parameter
    @param bar this is the second paramater
    """
    pass
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top