c108.utils
C108 Utilities and compatibility shims shared across the package.
Contains functions used by multiple modules to avoid circular imports.
class_name(obj, fully_qualified=False, fully_qualified_builtins=False, as_instance=False)
Get the class name of an object or a class.
Returns class name whether given an instance or the class itself.
For example, both class_name(10) and class_name(int) return 'int'.
This function safely handles edge cases including objects without standard attributes (class, name, module) by falling back to str() representation. Special handling for typing module constructs returns readable representations like 'List[int]' or 'Union[int, str]'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
An object or a class. |
required |
fully_qualified
|
bool
|
If true, returns the fully qualified name for user objects or classes. |
False
|
fully_qualified_builtins
|
bool
|
If true, returns the fully qualified name for builtin objects or classes. |
False
|
as_instance
|
bool
|
If True, class objects are treated as regular instances, returning their metaclass name (usually 'type'). If False (default), class objects return their own name. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The class name, or a string representation if standard attributes are unavailable. |
Examples:
Basic usage with a builtin instance: >>> class_name(10) 'int'
Fully qualified name for a builtin (when enabled): >>> class_name(10, fully_qualified_builtins=True) 'builtins.int'
User-defined class: instance and class object: >>> class C: ... >>> class_name(C()) 'C' >>> class_name(C, fully_qualified=True) 'c108.utils.C'
Treating classes as instances: >>> class_name(int) 'int' >>> class_name(int, as_instance=True) 'type'
Typing constructs with readable representations: >>> from typing import List, Union >>> class_name(List[int]) 'List[int]' >>> class_name(Union[int, str]) 'Union[int, str]'
Source code in c108/utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |