1

I need some help/advice on the best approach to do this. I have a block of code in Python, which is basically more or less the same line (a function/method applied to different variables). Now, sometimes, depending on other things, some of those variables may not exist, causing the lines that have any of them to throw an exception.

My goal is to run the block of code allowing the lines with the existing variables to run and the others to be skipped. I have a million workarounds for this, but i wanna see if there is a "clean" way, like, using a try/exception for the "entire" block.

My idea is to do something like this, where a1, a2, a3, etc., are the variables that could or could not exist:

try:
    functionx(a1)
    functionx(a2)
    functionx(a3)
    ...
except:

And somehow, to get that try/except wrap to skip the ones that don't exist, but execute the ones that exist.

Any idea?

Edit: The point is to see if, somehow, i can make the try to "continue" after it gets an exception inside its code block instead of breaking and going to the except.

Ghost
  • 1,426
  • 5
  • 19
  • 38
  • What do you mean "variables may not exist"? Are they `None`? – 001 Mar 08 '22 at 19:32
  • Not defined. Doesn't really matter, assume the line will throw an exception. My point is to know if i can, somehow, make the try call to "continue" with the rest of the block when it throws an exception instead of breaking. – Ghost Mar 08 '22 at 19:45

3 Answers3

3

Based on this answer, you can call your function only after checking the variable exists like so:

functionx(a1) if 'a1' in locals() else None
functionx(a2) if 'a2' in locals() else None
functionx(a3) if 'a3' in locals() else None
Martin Tovmassian
  • 1,010
  • 1
  • 10
  • 19
  • Yep, that's one of the workarounds.. i've also just initialized the variable empty, but i wanna see if there is a cleaner way to do it (like with a single try call). – Ghost Mar 08 '22 at 19:43
  • 1
    Well, gave it a couple thoughts and i'll just stick to this solution, thanks! – Ghost Mar 09 '22 at 15:41
2

Assuming you meant some vars might be None: only run your function on vars that aren't None:

for non_null_var in [v for v in [a1, a2, a3, ...] if v is not None]:
  functionx(non_null_var)

map & filter version:

map(functionx, filter(lambda x: x is not None, [a1, a2, a3, ...]))

If you really mean they aren't defined at all, then, modifying Martin's answer:

for non_null_var in [v for v in ['a1', 'a2', 'a3', '...'] if v is in locals() and locals()[v] is not None]:
      functionx(locals()[v])
Danielle M.
  • 3,607
  • 1
  • 14
  • 31
  • Yes, that's another workaround i've used, but please read the description again (i added some edit to point on that). I'm looking for a solution following the layout i wrote (a way to make the try-catch to "continue" after it finds an exception instead of just breaking to tho except) – Ghost Mar 08 '22 at 19:51
0

Maybe something like *args or **kwargs will do the trick.

def apply_on(*args):
    for arg in args:
    try:
        functionx(arg)
    except:
        pass

You could then call like:

apply_on(1, 2, 3, 4)

Using kwargs if the name of the "variable" is somehow important:

def apply_on(**kargs):
    for name, arg in args.items():
        try:
            functionx(arg)
        except:
            pass

Usually it is not clean to assume that variables exist or not. Pick some other data structure in this case, like list or dictionary, depending on your use case.