for a hard-coded version that works decently with 'well behaved' decorators.
It must be declared after function. if function gets rebound later the changes updated here.
def get_name_doc():
# global function # this is optional but makes your intent a bit more clear.
return function.__name__, function.__doc__
This is a rather nasty hack, in that it abuses the way default args work. It will used whatever function is bound to at the time this function is 'initialized', and remember it even if the function gets rebound. Calling it with args is going to lead to interesting results.
def get_name_doc(fn=function):
return fn.__name__, fn.__doc__
and a dynamic one that still hard coded but does update on function getting called with an argument of True. Basically this version will only update when told to do so.
def get_name_doc(update=False):
global fn
if update:
fn = function
return fn.__name__, fn.__doc__
Now of course there are decorator examples to this too.
@decorator # applying the decorator decorator to make it well behaved
def print_name_doc(fn, *args, **kwargs):
def inner(*args, **kwargs):
print(fn.__doc__, fn.__name__) # im assuming you just want to print in this case
return fn(*args, **kwargs)
return inner
you should read on the decorator decorator (atleast).
Look at NamedTuple source (from the collections module) as it involves noting being hard-coded. Sadly the named tuple code is rather weird. It is a string format used with eval rather then traditional code, but it works really neatly. This seems to be the most promising variant.
You might be able to do this with metaclasess too, which leads to neat code, but rather nasty stuff hidden behind the scenes, which you need to code. This id advise against
Im suspecting that there probably is an easier way than going into inspection/reflection/templates/metaclasess by simply adding the following line at the end of the module.
help(<module>)
where is the name of the module you are working on (a string). Or even the variable __name__. This could be done in the __init__.py file too if working with multiple modules
or on individual classes too i think.