MCPcopy Index your code
hub / github.com/numpy/numpy / __setattr__

Method __setattr__

numpy/_core/records.py:449–484  ·  view source on GitHub ↗
(self, attr, val)

Source from the content-addressed store, hash-verified

447 # Undo any "setting" of the attribute and do a setfield
448 # Thus, you can't create attributes on-the-fly that are field names.
449 def __setattr__(self, attr, val):
450
451 # Automatically convert (void) structured types to records
452 # (but not non-void structures, subarrays, or non-structured voids)
453 if (
454 attr == 'dtype' and
455 issubclass(val.type, nt.void) and
456 val.names is not None
457 ):
458 val = sb.dtype((record, val))
459
460 newattr = attr not in self.__dict__
461 try:
462 ret = object.__setattr__(self, attr, val)
463 except Exception:
464 fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
465 if attr not in fielddict:
466 raise
467 else:
468 fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
469 if attr not in fielddict:
470 return ret
471 if newattr:
472 # We just added this one or this setattr worked on an
473 # internal attribute.
474 try:
475 object.__delattr__(self, attr)
476 except Exception:
477 return ret
478 try:
479 res = fielddict[attr][:2]
480 except (TypeError, KeyError) as e:
481 raise AttributeError(
482 f"record array has no attribute {attr}"
483 ) from e
484 return self.setfield(val, *res)
485
486 def __getitem__(self, indx):
487 obj = super().__getitem__(indx)

Callers

nothing calls this directly

Calls 3

dtypeMethod · 0.45
__setattr__Method · 0.45
__getattribute__Method · 0.45

Tested by

no test coverage detected