Skip to content

Commit fc3fd6b

Browse files
r-barnesfacebook-github-bot
authored andcommitted
Replace hasattr with getattr in pytorch/opacus/opacus/data_loader.py
Summary: The pattern ``` X.Y if hasattr(X, "Y") else Z ``` can be replaced with ``` getattr(X, "Y", Z) ``` The [getattr](https://www.w3schools.com/python/ref_func_getattr.asp) function gives more succinct code than the [hasattr](https://www.w3schools.com/python/ref_func_hasattr.asp) function. Please use it when appropriate. **This diff is very low risk. Green tests indicate that you can safely Accept & Ship.** Differential Revision: D44886833 fbshipit-source-id: 1e5b91a1aaace39425468b0c777fb03e59a6ec28
1 parent 93dd307 commit fc3fd6b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

opacus/data_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def shape_safe(x: Any) -> Tuple:
7070
Returns:
7171
``x.shape`` if attribute exists, empty tuple otherwise
7272
"""
73-
return x.shape if hasattr(x, "shape") else ()
73+
return getattr(x, "shape", ())
7474

7575

7676
def dtype_safe(x: Any) -> Union[torch.dtype, Type]:
@@ -83,7 +83,7 @@ def dtype_safe(x: Any) -> Union[torch.dtype, Type]:
8383
Returns:
8484
``x.dtype`` if attribute exists, type of x otherwise
8585
"""
86-
return x.dtype if hasattr(x, "dtype") else type(x)
86+
return getattr(x, "dtype", type(x))
8787

8888

8989
class DPDataLoader(DataLoader):

0 commit comments

Comments
 (0)