0

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 ?

Kobi K
  • 7,743
  • 6
  • 42
  • 86
Numerobis
  • 3
  • 3
  • I don't understand what `Responsible` is supposed to mean here. Is it some class you defined somewhere else? Python variables do not have type; values do. – Karl Knechtel Jan 26 '14 at 11:51
  • Just for the record many IDEs support [epydoc](http://epydoc.sourceforge.net/fields.html). You can use this markup language in your docstring to get more warnings and tips on parameter types even in complicated functions and modules. – Mehdi Jan 26 '14 at 12:58

2 Answers2

0

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.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • I already knew the thing for the docstring, that's not the issue here. My problem is that I would like to be able to have a helptip with more explicit argument names without changing the ones in my code ... And I really don't know if it's possible or not ! – Numerobis Jan 26 '14 at 11:52
  • No, that isn't possible, the tooltip is automatically generated. You can use `*args`; I have edited my question with this option, but you still get a first line. – jonrsharpe Jan 26 '14 at 11:55
  • Thanks, that makes the deal with what I wanted to do ! – Numerobis Jan 26 '14 at 12:04
0

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
zmo
  • 24,463
  • 4
  • 54
  • 90
  • Ok thanks, so it's impossible for me to do what I want ... I heard about the Zen of Python, and what you explain at the end of your answer is exactly the problem I wanted to solve ... I'll deal with it, but don't worry, my args name are not arg1 ..., they are more explicit than that ! Thanks for your answer ! – Numerobis Jan 26 '14 at 11:56