Python Forum

Full Version: about help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi, sorry additional question, how to print all "__text_signature__",
I tried this but failed
import os
for di in dir(os):
    print(di)
    print(di.__text_signature__)
Error:
print(di.__text_signature__) AttributeError: 'str' object has no attribute '__text_signature__'
dir() returns attributes names, not the attributes themselves. Attribute names are str objects. str objects do not have a __text_signature__ attribute. It is likely a module has attributes that are not functions or methods. Only functions or methods may have __text_signature__ attributes, and not even all of those.

You should read about the inspect module. It provides a better interface for getting the information you are after.

https://docs.python.org/3/library/inspect.html

In this example I display all the functions/methods in the os module that have signatures.
import inspect
import os

for name, item in inspect.getmembers(os, predicate=inspect.isroutine):  # True if function or method
    try:
        signature = inspect.signature(item)
        module = inspect.getmodule(item).__name__
        print(f"{module}.{name}{signature}")
    except:
        pass
Small sampling of output when run on Windows. Note that not all functions are in the "os" module. Some are from "nt" which is a Python binding to some Windows os-like functions. The "nt" functions are accessed through the "os" module, but not defined there.
Output:
collections.abc._check_methods(C, *methods) os._execvpe(file, args, env=None) os._exists(name) nt._exit(status) os._fspath(path) ... nt.dup(fd, /) nt.dup2(fd, fd2, inheritable=True) os.execl(file, *args) os.execle(file, *args)
To learn about a module you should really read the documentation. I'm pretty sure you should not be using os._check_methods() or any of the ._functions.
(Sep-27-2022, 08:04 PM)deanhystad Wrote: [ -> ]dir() returns attributes names, not the attributes themselves. Attribute names are str objects. str objects do not have a __text_signature__ attribute. It is likely a module has attributes that are not functions or methods. Only functions or methods may have __text_signature__ attributes, and not even all of those.

You should read about the inspect module. It provides a better interface for getting the information you are after.

https://docs.python.org/3/library/inspect.html

In this example I display all the functions/methods in the os module that have signatures.
import inspect
import os

for name, item in inspect.getmembers(os, predicate=inspect.isroutine):  # True if function or method
    try:
        signature = inspect.signature(item)
        module = inspect.getmodule(item).__name__
        print(f"{module}.{name}{signature}")
    except:
        pass
THANK YOUUUUUUUUUUUUUU
Pages: 1 2