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__') 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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Gee, as soon as I finished writing the example I saw it, jeje
2

Yes, like this:

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

You just use or instead of ||

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.