You are on page 1of 1

num -= 1

return res

You can also sleep for a period of time before starting your execution. Note: this won't actually allow your program
to interrupt the computation happening inside the C function, but it will allow your main thread to continue after
the spawn, which is what you may expect.

def calc_fact(num):
sleep(0.001)
math.factorial(num)

Section 200.11: Multiple return


Function xyz returns two values a and b:

def xyz():
return a, b

Code calling xyz stores result into one variable assuming xyz returns only one value:

t = xyz()

Value of t is actually a tuple (a, b) so any action on t assuming it is not a tuple may fail deep in the code with a an
unexpected error about tuples.

TypeError: type tuple doesn't define ... method

The fix would be to do:

a, b = xyz()

Beginners will have trouble finding the reason of this message by only reading the tuple error message !

Section 200.12: Pythonic JSON keys


my_var = 'bla';
api_key = 'key';
...lots of code here...
params = {"language": "en", my_var: api_key}

If you are used to JavaScript, variable evaluation in Python dictionaries won't be what you expect it to be. This
statement in JavaScript would result in the params object as follows:

{
"language": "en",
"my_var": "key"
}

In Python, however, it would result in the following dictionary:

{
"language": "en",
"bla": "key"

GoalKicker.com – Python® Notes for Professionals 774

You might also like