https://pantsbuild.org/ logo
#development
Title
# development
r

rough-minister-58256

08/16/2016, 8:49 PM
Copy code
>>> import itertools
>>> def datatype(type_name, params):
...   class DataType(object):
...     __slots__ = tuple(params)
...     def __init__(self, *args):
...       assert len(args) == len(params), 'invalid input'
...       for kw, v in itertools.izip(params, args):
...         setattr(self, kw, v)
...   DataType.__name__ = type_name
...   return DataType
...
>>> RustLibrary = datatype('RustLibrary', ['sources', 'dependencies'])
>>> rl = RustLibrary([1,2,3], [4,5,6])
>>> rl.sources
[1, 2, 3]
>>> rl.dependencies
[4, 5, 6]
>>> rl.__slots__
('sources', 'dependencies')
>>> rl.__dict__
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'RustLibrary' object has no attribute ‘__dict__’
>>> isinstance(rl, RustLibrary)
True