Special Method Protocols
__new__() — object lifecycle protocol
Allocate or choose the instance before initialization. __new__ is implicitly static; if it returns an instance of cls, initialization follows. Returning another type skips cls.__init__. Useful for immutable subclasses. The displayed signature is the concrete example contract; constructor or call parameters depend on the class.
Formal Syntax & Structure
python syntaxpython
def __new__(cls, value):Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| value | str | Constructor input for this example. |
Return Value
object
Created or selected object.
Working Example
Example Snippetpython
class Label(str): def __new__(cls, value): return super().__new__(cls, value.strip())print(str(Label(" ready ")))