2

I have a function like this:

def __init__(self, out_file='out.csv', tmp_folder=None):
    self.tmp_folder = tmp_folder if tmp_folder != None else join(getcwd(), '__tmp__') 

I was wondering if ther exists a smarter way to do it, something like js:

    self.tmp_folder = tmp_folder || join(getcwd(), '__tmp__') 
1
  • 3
    tmp_folder or join(getcwd(), '__tmp__')
    – mshsayem
    Commented Apr 30, 2014 at 2:18

2 Answers 2

3

Since None evaluates to False, you could always do:

def __init__(self, out_file='out.csv', tmp_folder=None):
    self.tmp_folder = tmp_folder or join(getcwd(), '__tmp__')

Note however that this will assign self.tmp_folder to join(getcwd(), '__tmp__') if tmp_folder is any falsey value (False, 0, [], {}, etc.)

1
  • Gee, as soon as I finished writing the example I saw it, jeje
    – opensas
    Commented Apr 30, 2014 at 2:19
2

Yes, like this:

self.tmp_folder = tmp_folder or join(getcwd(), '__tmp__')

You just use or instead of ||

Not the answer you're looking for? Browse other questions tagged or ask your own question.