StdClass
StdClass
Section titled “StdClass”StdClass is a lightweight, generic container that turns keyword arguments into object attributes. It provides a simple way to create structured data objects on the fly without defining a formal class — similar to an anonymous object or a plain data-transfer object.
Use StdClass when you need a quick, flexible container for passing grouped values around your application — configuration bundles, intermediate results, or any scenario where a full class definition would be overkill.
Import
Section titled “Import”from orionis.support.standard.std import StdClassCreating an Instance
Section titled “Creating an Instance”Pass any keyword arguments to the constructor. Each key becomes an attribute on the resulting object:
obj = StdClass(name="Orionis", version=1, debug=True)
obj.name # "Orionis"obj.version # 1obj.debug # TrueAn empty instance has no attributes:
obj = StdClass()obj.toDict() # {}From a Dictionary
Section titled “From a Dictionary”The class method fromDict creates an instance from an existing dictionary:
data = {"host": "localhost", "port": 5432}
config = StdClass.fromDict(data)config.host # "localhost"config.port # 5432A fromDict → toDict roundtrip preserves the original data:
original = {"a": 1, "b": "hello"}StdClass.fromDict(original).toDict() == original # TrueManaging Attributes
Section titled “Managing Attributes”Reading Attributes
Section titled “Reading Attributes”Access attributes with standard dot notation. Use hasattr to check for existence:
obj = StdClass(color="blue", count=5)
obj.color # "blue"hasattr(obj, "color") # Truehasattr(obj, "missing") # FalseAdding and Updating Attributes
Section titled “Adding and Updating Attributes”The update method adds new attributes or overwrites existing ones:
obj = StdClass(x=1)
obj.update(y=2, z=3) # add new attributesobj.update(x=99) # overwrite existing
obj.toDict() # {"x": 99, "y": 2, "z": 3}Multiple update calls accumulate attributes:
obj = StdClass()obj.update(a=1)obj.update(b=2)obj.update(c=3)obj.toDict() # {"a": 1, "b": 2, "c": 3}You can also set attributes directly:
obj = StdClass(x=1)obj.x = 99obj.x # 99Protected Names
Section titled “Protected Names”update rejects attribute names that conflict with the class interface:
- Dunder names (
__name__,__init__, etc.) — raisesValueError - Existing method names (
toDict,update,remove,fromDict) — raisesValueError
obj = StdClass()
obj.update(__reserved__="bad") # ValueErrorobj.update(toDict="conflict") # ValueErrorobj.update(remove="conflict") # ValueErrorRemoving Attributes
Section titled “Removing Attributes”The remove method deletes one or more attributes by name. Raises AttributeError if any attribute does not exist:
obj = StdClass(a=1, b=2, c=3)
obj.remove("a")obj.toDict() # {"b": 2, "c": 3}
obj.remove("b", "c")obj.toDict() # {}obj = StdClass()obj.remove("missing") # AttributeError: Attribute 'missing' not foundConverting to Dictionary
Section titled “Converting to Dictionary”toDict returns a shallow copy of all attributes as a plain dict. Mutating the returned dictionary does not affect the original object:
obj = StdClass(x=1, flag=True)
d = obj.toDict() # {"x": 1, "flag": True}d["x"] = 999obj.x # 1 — unchangedEquality and Hashing
Section titled “Equality and Hashing”Two StdClass instances are equal when they have the same attributes with the same values:
a = StdClass(x=1, y=2)b = StdClass(x=1, y=2)
a == b # TrueDifferent values, different keys, or comparison with non-StdClass objects all produce False:
StdClass(x=1) == StdClass(x=2) # FalseStdClass(x=1) == StdClass(y=1) # FalseStdClass(x=1) == {"x": 1} # FalseStdClass instances are hashable, so they can be used as dictionary keys or in sets. Instances with identical attributes produce the same hash:
a = StdClass(x=1)b = StdClass(x=1)
{a, b} # set with 1 element{a: "value"} # works as dict keyModifying attributes changes the hash value.
String Representation
Section titled “String Representation”repr() includes the class name and all attributes — useful for debugging:
obj = StdClass(name="Orionis", v=3)
repr(obj) # "StdClass({'name': 'Orionis', 'v': 3})"str(obj) # "{'name': 'Orionis', 'v': 3}"Method Reference
Section titled “Method Reference”| Method | Signature | Description |
|---|---|---|
__init__ | StdClass(**kwargs) | Creates an instance with keyword arguments as attributes |
fromDict | StdClass.fromDict(dict) → StdClass | Class method — creates an instance from a dictionary |
update | update(**kwargs) → None | Adds or overwrites attributes. Rejects dunder and method names |
remove | remove(*names) → None | Deletes attributes by name. Raises AttributeError if missing |
toDict | toDict() → dict | Returns a shallow copy of all attributes as a dictionary |