Skip to content

Commit

Permalink
Merge pull request #631 from Isaac-Flath/NestedAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 authored Oct 2, 2024
2 parents 2ff4004 + 7044728 commit cc75f59
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 54 deletions.
7 changes: 5 additions & 2 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,11 @@ def only(o):
def nested_attr(o, attr, default=None):
"Same as `getattr`, but if `attr` includes a `.`, then looks inside nested objects"
try:
for a in attr.split("."): o = getattr(o, a)
except AttributeError: return default
for a in attr.split("."):
if hasattr(o,a): o = getattr(o, a)
else: o = o[a]
except (AttributeError, KeyError,IndexError, TypeError, ValueError):
return default
return o

# %% ../nbs/01_basics.ipynb
Expand Down
Loading

0 comments on commit cc75f59

Please sign in to comment.