Sujet : Re: Dynamic classes
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 21. May 2025, 12:20:55
Autres entêtes
Organisation : Stefan Ram
Message-ID : <dynamic-20250521121804@ram.dialup.fu-berlin.de>
References : 1
Left Right <
olegsivokon@gmail.com> wrote or quoted:
I find that it's generally more convenient to do this using similar code:
def constructor(flag1, flag2):
class _Hidden:
That tracks, but I have this setup where I lay out a table to
map out my markup language [1] and then use some dynamic code
to spin up the classes based on that table [0], and I am not
really sure how your way would fit in with that.
[0]
globals_dict = globals()
for name, props in element_properties.items():
cls = type(name.capitalize(), (Element,), {'element_properties': props})
globals_dict[name.capitalize()] = cls
[1]
element_properties = {
'title': {
'level': IS_BLOCK,
'contents': CANT_CONTAIN_ELEMENTS,
'open_tag': None,
'close_tag': None,
'use_check': False,
},
'author': {
'level': IS_BLOCK,
'contents': CANT_CONTAIN_ELEMENTS,
'open_tag': None,
'close_tag': None,
'use_check': False,
},
'language': {
'level': IS_BLOCK,
'contents': CANT_CONTAIN_ELEMENTS,
'open_tag': None,
'close_tag': None,
'use_check': False,
},
'h1': {
'level': IS_BLOCK,
'contents': CAN_CONTAIN_ELEMENTS,
'open_tag': '<h1>',
'close_tag': '</h1>',
'use_check': True,
},
'toc': {
'level': IS_BLOCK,
'contents': CANT_CONTAIN_ELEMENTS,
'open_tag': None,
'close_tag': None,
'use_check': False,
},
'head': {
'level': IS_BLOCK,
'contents': CANT_CONTAIN_ELEMENTS,
'open_tag': None,
'close_tag': None,
'use_check': False,
},
'par': {
'level': IS_BLOCK,
. . .