Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do what you're looking to do, in the way you want to do it.</p> <p>From your description it seems like you could do something like this:</p> <pre><code>for tool in find_tools(): def __tool(*arg): validate_args(tool, args) return execute_tool(tool, args) __tool.__name__ = tool.name __tool.__doc__ = compile_docstring(tool) setattr(module, tool.name, __tool) </code></pre> <p>i.e. create the documentation string dynamically up-front when you create the function. Is the a reason why the docstring has to be dynamic from one call to <code>__doc__</code> to the next?</p> <p>Assuming there is, you'll have to wrap your function up in a class, using <code>__call__</code> to trigger the action.</p> <p>But even then you've got a problem. When help() is called to find the docstring, it is called on the class, not the instance, so this kind of thing:</p> <pre><code>class ToolWrapper(object): def __init__(self, tool): self.tool = tool self.__name__ = tool.name def _get_doc(self): return compile_docstring(self.tool) __doc__ = property(_get_doc) def __call__(self, *args): validate_args(args) return execute_tool(tool, args) </code></pre> <p>won't work, because properties are instance, not class attributes. You can make the doc property work by having it on a metaclass, rather than the class itself</p> <pre><code>for tool in find_tools(): # Build a custom meta-class to provide __doc__. class _ToolMetaclass(type): def _get_doc(self): return create_docstring(tool) __doc__ = property(_get_doc) # Build a callable class to wrap the tool. class _ToolWrapper(object): __metaclass__ = _ToolMetaclass def _get_doc(self): return create_docstring(tool) __doc__ = property(_get_doc) def __call__(self, *args): validate_args(tool, args) execute_tool(tool, args) # Add the tool to the module. setattr(module, tool.name, _ToolWrapper()) </code></pre> <p>Now you can do</p> <pre><code>help(my_tool_name) </code></pre> <p>and get the custom docstring, or</p> <pre><code>my_tool_name.__doc__ </code></pre> <p>for the same thing. The <code>__doc__</code> property is in the <code>_ToolWrapper</code> class is needed to trap the latter case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload