Create a new instance, based on *inst* with *changes* applied. :param inst: Instance of a class with ``attrs`` attributes. :param changes: Keyword changes in the new copy. :return: A copy of inst with *changes* incorporated. :raise TypeError: If *attr_name* couldn't be found
(inst, **changes)
| 342 | |
| 343 | |
| 344 | def evolve(inst, **changes): |
| 345 | """ |
| 346 | Create a new instance, based on *inst* with *changes* applied. |
| 347 | |
| 348 | :param inst: Instance of a class with ``attrs`` attributes. |
| 349 | :param changes: Keyword changes in the new copy. |
| 350 | |
| 351 | :return: A copy of inst with *changes* incorporated. |
| 352 | |
| 353 | :raise TypeError: If *attr_name* couldn't be found in the class |
| 354 | ``__init__``. |
| 355 | :raise attr.exceptions.NotAnAttrsClassError: If *cls* is not an ``attrs`` |
| 356 | class. |
| 357 | |
| 358 | .. versionadded:: 17.1.0 |
| 359 | """ |
| 360 | cls = inst.__class__ |
| 361 | attrs = fields(cls) |
| 362 | for a in attrs: |
| 363 | if not a.init: |
| 364 | continue |
| 365 | attr_name = a.name # To deal with private attributes. |
| 366 | init_name = attr_name if attr_name[0] != "_" else attr_name[1:] |
| 367 | if init_name not in changes: |
| 368 | changes[init_name] = getattr(inst, attr_name) |
| 369 | |
| 370 | return cls(**changes) |
| 371 | |
| 372 | |
| 373 | def resolve_types(cls, globalns=None, localns=None, attribs=None): |