Niag Ntawv
Niag Ntawv

Reputation: 337

call different class functions or a combo of class functions depending on arg in python

I have a subparser named app01. It has two arguments --delete-user, and --delete-hash. I've set the default function to call the function check_app

the check_app function is something like this:

def check_app(args):
    a = App()
    if args.user:
        # run function01 from App() class to delete user account
    elif args.hash:
        # run function02 from App() class delete user hash
    else:
        # run both functions to delete user and hash

How do i get that to work without having to add both function01 and function02 into the else block. Or is that the only way?

Upvotes: 1

Views: 29

Answers (1)

Harsh
Harsh

Reputation: 399

I think what code you have is decent.

If you want what you want this for purely aesthetic reasons, let me warn you that the code maintainability will drop substantially. You can see the answer given by Ronan B already seems pretty confusing and if you're the person trying to understand it, it feels like too much work to decipher.

But if you still want me to titillate your senses, I may as well suggest a simpler version:

def check_app(args):
    a = App()
    if not args.user:
        # run #02    < note #2
    if not args.hash:
        # run #01    < note #1

This is of course assuming that if both functions #1 and #2 need to run, no arguments are given, instead of both arguments being given, as is typically intuitive.

Upvotes: 1

Related Questions