Skip to content

Commit

Permalink
Replace hasattr with getattr in vision/fair/pytorch3d/pytorch3d/rende…
Browse files Browse the repository at this point in the history
…rer/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
  • Loading branch information
r-barnes authored and facebook-github-bot committed Apr 14, 2023
1 parent 355d633 commit 1af6bf4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pytorch3d/renderer/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ def in_ndc(self):
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]
Expand Down

0 comments on commit 1af6bf4

Please sign in to comment.