clojure - Meta data in python -
i've noted in languages, clojure example, can assign meta-data objects.
in python, can this.
class meta_dict(dict):pass a={'name': "bill", 'age':35} meta_a = meta_dict(a) meta_a.secret_meta_data='42' meta_a==a true secret_meta_data_dict=meta_a.__dict__ original_dict=dict(meta_a)
i wondering if reasonable pattern follow when need have data in particular form want other data follow along gracefully.
no comment on ...reasonable pattern follow...
here way using __metaclass__
>>> class metafoo(type): def __new__(mcs, name, bases, dict): dict['foo'] = 'super secret foo' return type.__new__(mcs, name, bases, dict) >>> class wye(list): __metaclass__ = metafoo >>> class zee(dict): __metaclass__ = metafoo >>> y = wye() >>> y.append(1) >>> y.append(2) >>> y [1, 2] >>> y.foo 'super secret foo' >>> z = zee({1:2, 3:4}) >>> z[1] 2 >>> z.items() [(1, 2), (3, 4)] >>> z.foo 'super secret foo' >>>
Comments
Post a Comment