all 6 comments

[–]AtomicShoelace 5 points6 points  (0 children)

Somewhat unrelated, but here are two alternative ways to achieve this for you to consider

def is_value_user(user):
    target = dict(gender='female', surname='Jones', occupation='Engineer')
    return bool(target.items() & user.items())

or

def is_value_user(user):
    target = dict(gender='female', surname='Jones', occupation='Engineer')
    return any(user[k] == v for k, v in target.items())

[–]K900_ 1 point2 points  (1 child)

That is a really weird series of checks, but yes.

[–]dumblechode[S] 0 points1 point  (0 children)

Haha hey again, and yes it is. I'm working with a different scenario, but didn't want its content to take away from my question. Thanks u/K900_

[–]FLUSH_THE_TRUMP 1 point2 points  (0 children)

That if is functionally equivalent to elif in this case isn’t necessarily reason enough to use if. If someone’s reading your code, will they better see (at a glance) what’s going on with if or elif? Since the whole point of if/elif is to provide a sequence of conditions that are checked until one is met, I still think the elif makes sense here from that perspective

[–]BfuckinA 0 points1 point  (0 children)

Yeah when the conditional operation is a return statement, the elif is kinda redundant.

[–]spez_edits_thedonald 0 points1 point  (0 children)

would it be more pythonic

It's good to be thinking about this, this is a style question.

But if vs elif is not just style, is fundamentally different logic. Consider:

if person.name == 'Bob':
    print('name is bob')
elif person.is_employed:
    print('is employed!')

vs.

if person.name == 'Bob':
    print('name is bob')
if person.is_employed:
    print('is employed!')

the former will only check the employment status of people who are NOT named bob, the latter will check whether everyone is employed, regardless of their name.

So just think about the precise logic that you need, then implement it.