Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring "x if x is not None else y" to "x or y"

I came across to a piece of Python Code which technically looks like this:

def bar(b):
    return b*2

class MyClass(object):
    def __init__(self, a):
        self.a = a
        self.b = 10

    def foo(self, b=None):
        return bar(b if b is not None else self.b)

So the class has b as a fixed attribute, which is used as default input for a classmethod foo, if it is called without argument.

Please assume that b in this case is expected to be a float.

Please ignore unused self.a, it is used otherwise - i just wanted to make sure that you see that there are attributes settable by the constructor.

The usage would look like this:

c = MyClass(2)
c.foo(3)  # returns 3*2 = 6
c.foo()  # returns 10*2 = 20

In this case, I was wondering about the foo-method. Is this a good way to implement self.b as default value for b?

Is one of the following two suggestions by me more or less desireable? If so: why?

def foo(self, b=None):
    return bar(b or self.b)

or

def foo(self, b=self.b):
    return bar(b)
like image 918
Nras Avatar asked Aug 31 '25 02:08

Nras


1 Answers

Stick with the first option:

def foo(self, b=None):
    return bar(b if b is not None else self.b)

If you use the second option:

def foo(self, b=None):
    return bar(b or self.b)

then you will alter the behaviour of x.foo(0), because zero is also falsey.

The third option is not an option:

def foo(self, b=self.b):
    return bar(b)

self does not exist when the method foo() is defined, so you cannot use self.b as a default argument. The definition will simply fail; or worse, it will try and use whatever is the current value of self when the definition is executed.

like image 187
khelwood Avatar answered Sep 02 '25 14:09

khelwood