Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
about help
#11
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__'
Reply
#12
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.
kucingkembar likes this post
Reply
#13
(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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020