Python’s empty function does not require a pass statement? -


i stumbled upon interesting , unexpected feature of python:

def fun():     """foo’s docstring""" 

is valid function? according pep 257, “a docstring string literal occurs first statement in module, function, class, or method definition” meaning docstring considered statement?

i have expected @ least pass statement required here. above example contradicts python zen of “explicit better implicit” pass states explicit intent, , docstring not.

can shed light on intent?

a string literal other literal. works if put in integer:

def func():     1 

however, doesn't work if use comment:

def func():     # test  # indentationerror: expected indented block 

even though it's added docstring (saved in __doc__ attribute) it's function level constant:

def func():     """i'm function"""  >>> func.__code__.co_consts ("i'm function", none) 

so presence of string literal content of function doesn't change how function "parsed" , "compiled" itself. well, apart fact got not-none __doc__ attribute.

it's handy abstractmethods (see example "body of abstract method in python"), don't need actual function body.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -