python getattr built-in method executes default arguments
Asked Answered
S

1

7

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.

Saxen answered 26/7, 2016 at 12:23 Comment(0)
I
12

Before getattr gets executed, all the passed parameters have to be evaluated. func() is one of those parameters and an attempt to evaluate it will execute the print statement. Whether the attribute will be found or not, func() must be evaluated apriori.

This isn't peculiar to getattr, it's how functions and their parameters work in Python.


Consider the following:

>>> def does_nothing(any_arg): pass
...
>>> def f(): print("I'll get printed")
...
>>>
>>> does_nothing(f())
I'll get printed

Function does_nothing actually does nothing with the passed parameter. But the parameter has to be evaluated before the function call can go through.


The print statement however will not affect the outcome of getattr; sort of a side effect. In the event the attribute is not found the return value of the function is used.

Isauraisbel answered 26/7, 2016 at 12:39 Comment(5)
I think this will be an overhead of adding extra function and instead I think I should go with hasattr check. Nevertheless Thanks.Saxen
If you don't want func() to be evaluated when a has attr param, you can change to: getattr(a, 'param', None) or func(). Any valid code after or becomes lazy.Pumpkin
Thanks @Chang.Jian, this is useful trick. how can I miss this.Saxen
It's not completely equivalent. The code after or will also run if the value before the or is falsy, like 0 or FalseEbert
The downside of this "lazy or" solution is that will also trigger func() if a.param does exist but it evaluates to False (in case if it's 0, "", None, etc). The solution is to either use try/except AttributeError or a.param if hasattr(a, "param") else func(). Note that in that case python will get a.param twice and sometimes it can be undesirable - for these cases I came up with this snippet v if (v := getattr(a, "param", ...)) == ... else func().Mayworm

© 2022 - 2024 — McMap. All rights reserved.