[Tutor] Using string formatting inside function docstrings
boB Stepp
robertvstepp at gmail.com
Sat Apr 11 02:20:41 EDT 2020
On Sat, Apr 11, 2020 at 12:57 AM boB Stepp <robertvstepp at gmail.com> wrote:
> This runs:
>
> def fmt(*args):
> """Prints arguments of %s.""" % args[0]
> for i, arg in enumerate(args):
> print("arg #%s = %s " % (i, arg), end="")
> print()
>
>
> fmt("Robert", "V.", "Stepp")
>
> Giving:
>
> bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 s*
> arg #0 = Robert arg #1 = V. arg #2 = Stepp
>
> But my original idea of using this for the docstring:
>
> """Prints arguments of %s.""" % args
>
> Throws an exception:
>
> bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 s*
> Traceback (most recent call last):
> File "str_fmt.py", line 11, in <module>
> fmt("Robert", "V.", "Stepp")
> File "str_fmt.py", line 5, in fmt
> """Prints arguments of %s.""" % args
> TypeError: not all arguments converted during string formatting
Ah, I see why now. There is only one "%s", but it is expecting three
"%s" based on the number of arguments passed in. It is too late to be
trying to be too clever!
--
boB
More information about the Tutor
mailing list