Skip to content

Commit

Permalink
Replace hasattr with getattr in vision/fair/pytorchvideo/pytorchvideo…
Browse files Browse the repository at this point in the history
…/layers/attention.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: D44886897

fbshipit-source-id: f07821a6af6114cf74f9b10125593a19a1b8bdcb
  • Loading branch information
r-barnes authored and facebook-github-bot committed Apr 14, 2023
1 parent f7e7a88 commit 28fe037
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pytorchvideo/layers/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,17 @@ def __init__(
self._attention_pool_q = _AttentionPool(
self.pool_q,
has_cls_embed=self.has_cls_embed,
norm=self.norm_q if hasattr(self, "norm_q") else None,
norm=getattr(self, "norm_q", None),
)
self._attention_pool_k = _AttentionPool(
self.pool_k,
has_cls_embed=self.has_cls_embed,
norm=self.norm_k if hasattr(self, "norm_k") else None,
norm=getattr(self, "norm_k", None),
)
self._attention_pool_v = _AttentionPool(
self.pool_v,
has_cls_embed=self.has_cls_embed,
norm=self.norm_v if hasattr(self, "norm_v") else None,
norm=getattr(self, "norm_v", None),
)

def _qkv_proj(
Expand Down

0 comments on commit 28fe037

Please sign in to comment.