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

Method create_init

Solutions/8_3/structure.py:43–53  ·  view source on GitHub ↗

Create an __init__ method from _fields

(cls)

Source from the content-addressed store, hash-verified

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

Callers 1

validate_attributesFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected