masterPiece93 / PythonEssentials

0 stars 0 forks source link

Metaclasses #8

Open masterPiece93 opened 7 months ago

masterPiece93 commented 7 months ago

Index

  1. use of type for creating dynamic classes
masterPiece93 commented 7 months ago

use of type for creating dynamic classes

1.

cls = type('DummyClass'
            ,   (object,)
            ,   {
                '__init__': lambda self, args: None
                ,   'int_cls_attr': 1234567
                ,   'str_cls_attr': 'ankit yadav'
            }
)

obj = cls(...)
print(cls)
print(cls.int_cls_attr)
print(cls.str_cls_attr)
print(obj)