I dont know if this is something an expected behavior of getattr
built_in method.
getattr
executes the default(3rd) argument as well even if the actual argument(2nd) satisfies the condition.
Example:
def func():
print('In Function')
class A:
def __init__(self):
self.param = 12
a = A()
When I run getattr(a, 'param', func())
it gives this result:
In Function
12
Note the In Function
which I don't want.
But it works perfectly fine when I execute getattr(a, 'param1', func())
i.e output
In Function
But I only want result as 12
if satisfied the condition. Please let me know why getattr
has such behaviour and can we stop them of doing it (that is not to execute 3rd arg if has 2nd argument), would be appreciated if share the alternate way of doing it in Pythonic way.
One thing that comes in mind first is to check if param1
exist using hasattr
and then do the needful.
hasattr
check. Nevertheless Thanks. – Saxen