tags:

views:

247

answers:

3

Is there a way in python to turn a try/except into a single line?

something like...

b = 'some variable'
a = c | b #try statement goes here

Where b is a declared variable and c is not... so c would throw an error and a would become b...

+5  A: 

There is no way to compress a try/except block onto a single line in Python.

Also, it is a bad thing not to know whether a variable exists in Python, like you would in some other dynamic languages. The safer way (and the prevailing style) is to set all variables to something. If they might not get set, set them to None first (or 0 or '' or something if it is more applicable.)


If you do assign all the names you are interested in first, you do have options.

  • The best option is an if statement.

    c = None
    b = [1, 2]
    
    
    if c is None:
        a = b
    else:
        a = c
    
  • The one-liner option is a conditional expression.

    c = None
    b = [1, 2]
    a = c if c is not None else b
    
  • Some people abuse the short-circuiting behavior or or to do this. This is error prone so I never use it.

    c = None
    b = [1, 2]
    a = c or b
    

    consider the following case though

    c = []
    b = [1, 2]
    a = c or b
    

    in this case a probably should be [] but it is [1, 2] because [] is false in a boolean context. Because there are lots of values that can be false, I don't use the or trick. (This is the same problem people run into when they say if foo: when they mean if foo is not None:.)

Mike Graham
Thanks. The problem is that its actually a django model.objects.get query i am trying to test. the .get returns an error if no data is found... it doesn't return None (which annoys me)
Brant
@Brant, so why can't you catch the error? why one line?
Anurag Uniyal
@Brant, Okay, that situation is a bit different than checking if a variable is set (no variables are declared in Python). The typical style in Python is to prefer raising exceptions to returning errors as values, which many of us actually love. Having to check the return code of an operation every time and having a hard time tracking down errors if I don't is something I definitely don't miss about C when writing Python. In any event, though it's been discussed, there is no one-line syntax for a `try`/`except` block. Luckily lines are cheap, so the 4-line solution should work for you. ;-)
Mike Graham
It is part of a large set of tuples within a dict... I was just trying to shorten things up a bit
Brant
Don't use `get` if you don't want an exception. Use `filter` instead.
jcdyer
+2  A: 

You mentioned that you're using django. If it makes sense for what you're doing you might want to use:

my_instance, created = MyModel.objects.get_or_create()

created will be True or False. Maybe this will help you.

blokeley
A: 

You can do it by accessing the namespace dict using vars(), locals(), or globals(), whichever is most appropriate for your situation.

>>> b = 'some variable'
>>> a = vars().get('c', b)
jcdyer
This doesn't work exactly the same as checking whether a variable is set (though it does if you are interested in a particular scope.) Also, ewwwwwwww.....
Mike Graham