Allow hasattr() function
See original GitHub issueWhat’s wrong
WPS421 forbids calling some built-in functions, which includes hasattr()
.
How it should be
The function hasattr()
should be allowed. There are certain scenarios where using hasattr()
makes sense, and sometimes is even faster (as even outlined in the referenced video @ 13:34-14:15 mark)
For code readability, if hasattr(foo, 'bar'):
is cleaner than if getattr(foo, 'bar', None):
.
I believe there was an argument against using hasattr()
in python2, but I don’t believe the same argument is valid for python3.
Flake8 version and plugins
n/a
pip information
n/a
OS information
n/a
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Is hasattr() a method? [closed] - Stack Overflow
Python functions, added to a class, become methods automatically when you access them as an attribute on an instance of the class:
Read more >Python hasattr() method - GeeksforGeeks
Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return...
Read more >How to use Python hasattr() method? - AskPython
The hasattr() method returns a boolean value i.e. either True or False depending on the presence of the attribute in the class.
Read more >Python hasattr() Builtin Function - Examples - Tutorial Kart
Python hasattr() builtin function is used to check if a Python object has a specific named attribute. In this tutorial, we will learn...
Read more >Why do we use the Python hasattr() function? - Toppr
The Python hasattr() built-in function is used to check if the specified object has the named attribute present within a class or not....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
These are good points. However,
getattr
outputswow
just likehasattr
does. By that logic,getattr
should be added to the FUNCTIONS_BLACKLIST as well then, yes?To be clear, I don’t advocate blacklisting
getattr
as there are semantic and performance reasons for using it.Semantically, I think that
if hasattr(A, 'foo')
is clearer thanif getattr(A, 'foo', False)
, which was the reasoning behind opening this issue. Do you agree?what @Dresdn says makes sense, one more reason in favor of allow
hasattr
is that the logic behind this one is performed by callinggetattr
and catching AttributteError, so, at the end both funcions are base on the same logic, It does not make sense to allowgetattr
and and issue a warning onhasattr