From 1af6bf47681304b272650bb59310b3cfa41b347b Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Fri, 14 Apr 2023 04:24:54 -0700 Subject: [PATCH] Replace hasattr with getattr in vision/fair/pytorch3d/pytorch3d/renderer/cameras.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.** Reviewed By: bottler Differential Revision: D44886893 fbshipit-source-id: 86ba23e837217e1ebd64bf8e27d286257894839e --- pytorch3d/renderer/cameras.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch3d/renderer/cameras.py b/pytorch3d/renderer/cameras.py index 22df74a8..b072a0e0 100644 --- a/pytorch3d/renderer/cameras.py +++ b/pytorch3d/renderer/cameras.py @@ -375,14 +375,14 @@ class CamerasBase(TensorProperties): raise NotImplementedError() def get_znear(self): - return self.znear if hasattr(self, "znear") else None + return getattr(self, "znear", None) def get_image_size(self): """ Returns the image size, if provided, expected in the form of (height, width) The image size is used for conversion of projected points to screen coordinates. """ - return self.image_size if hasattr(self, "image_size") else None + return getattr(self, "image_size", None) def __getitem__( self, index: Union[int, List[int], torch.BoolTensor, torch.LongTensor]