[Tutor] Named function in a single line
Richard Damon
Richard at Damon-Family.org
Thu Sep 10 14:12:12 EDT 2020
On 9/10/20 1:34 PM, Manprit Singh wrote:
> Dear sir ,
>
> I have seen at so many places that smaller named functions are written in a
> single line . For example ;
>
> def multiply(a, b): # a function definition for multiplication of 2
> numbers
> return a * b
>
> The function definitions which are very small , normally which contain
> only a single line statement, like that one given above , are being found
> written in a single line at so many places . If I write the function given
> above in a single line , it would be written like this :
>
> def multiply(a, b) : return a * b
>
> PEP 8 allows us to write such small functions in a single line ?
>
> Regards
> Manprit Singh
One thing to remember that unless you are writing code to be actually
submitted to be part of python itself or the standard library, PEP 8 is
just guidance, not law.
To really follow the PEP 8 guidance, if the function is really something
useful, there should be a doc-string for it, and maybe even some type
hints (that may not be in PEP 8 but just good practice), and by the time
you add all that, it gets hard to be just a single line. Functions that
are really that simple might not be worth making functions for real
code, but do make good learning examples.
--
Richard Damon
More information about the Tutor
mailing list