MCPcopy Create free account
hub / github.com/dabeaz-course/python-mastery / create_init

Method create_init

Solutions/8_1/structure.py:44–54  ·  view source on GitHub ↗

Create an __init__ method from _fields

(cls)

Source from the content-addressed store, hash-verified

42
43 @classmethod
44 def create_init(cls):
45 '''
46 Create an __init__ method from _fields
47 '''
48 args = ','.join(cls._fields)
49 code = f'def __init__(self, {args}):\n'
50 for name in cls._fields:
51 code += f' self.{name} = {name}\n'
52 locs = { }
53 exec(code, locs)
54 cls.__init__ = locs['__init__']
55
56 @classmethod
57 def __init_subclass__(cls):

Callers 1

validate_attributesFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected