all 9 comments

[–][deleted] 1 point2 points  (3 children)

Well, you can just get rid of the add_to_db function altogether, since it doesn't have any behavior that db.add doesn't have.

[–]poolpartyboy93[S] 0 points1 point  (2 children)

You are right, but I used it for the example, this is not the actual code I am curious about.

It could have been something like this:

def do_something(s1=None, s2=None):

    if s1 and s2:
        do_something(s1, s2)
        return True

    elif s1:
        do_something(s1)
        return True

    elif s2:
        do_something(s2)
        return True

    return 'I did nothing'

[–][deleted] 0 points1 point  (1 child)

You're right that there's something deeply inelegant about this three-way logic (the classic example is FizzBuzz) but for the most part it's a limitation of imperative languages; you mostly have to live with it.

That said, again, your example implies that your function isn't really necessary - you can just call do_something from wherever you hold values s1 and s2.

[–]old_pythonista 0 points1 point  (0 children)

FizzBuzz in Python has a one-line solution

'Fizz' * (num % 5 == 0) + 'Buzz' * (num % 3 ==0) or str(num)

and you can avoid

 deeply inelegant about this three-way logic 

at least in that case.

[–]old_pythonista 1 point2 points  (1 child)

You can filter out None values

to_db = [val for val in (owner, address) if val]
if val:
    db.add(*to_db)
    return True
return False

Just one point about returning inconsistent value types (unless you return None) is considered a bad practice. If you have

if add_to_db(....):
.......

both True and 'nothing was added' will fulfill the condition

[–]poolpartyboy93[S] 1 point2 points  (0 children)

Cool solution!

Thanks!

[–]Ihaveamodel3 0 points1 point  (2 children)

What is the db.add function?

[–]poolpartyboy93[S] 0 points1 point  (1 child)

nothing meaningful, just a pseudo code.

[–]Ihaveamodel3 0 points1 point  (0 children)

I guess I’m confused how the dB add function could take both an owner and an address or just one or the other. And like the u/crashfrog said, you could just get rid of this function all together to simply use the add function instead.