Convert Python classes to strings and back. Useful for serialization, configuration files, and message passing.
pip install cls-strfrom cls_str import cls_to_str, str_to_cls
# Convert class to string
from http.server import SimpleHTTPRequestHandler
class_path = cls_to_str(SimpleHTTPRequestHandler)
print(class_path)
# Output: "http.server.SimpleHTTPRequestHandler"
# Convert string back to class
restored_class = str_to_cls(class_path)
assert restored_class is SimpleHTTPRequestHandlerclass Outer:
class Inner:
pass
# Works with nested classes too
path = cls_to_str(Outer.Inner)
print(path)
# Output: "your_module.Outer.Inner"
loaded_class = str_to_cls(path)
assert loaded_class is Outer.InnerMIT License