Pythonic way of constructing an array based on boolean arguments?

Viewed 103

I was just wondering how would a pythonic code look like to tackle this problem:

Suppose you have a function:

def do_stuff(a=True, b=True, c=True, d=True):

And inside that function you want to construct corresponding objects:

elements = []
if a:
    elements.append(A())
if b:
    elements.append(B())
if c:
    elements.append(C())
if d:
    elements.append(D())

Are there any more beautiful ways of writing this code? If not maybe optional parameters is not "the way to go"?

5 Answers
Related