r/pythonhelp • u/biggAzzZuck • Sep 09 '21
Help understanding functions
Number of days per month
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap(x):
return x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)
def days_in_month(year, month):
if not 1 <= month <= 12:
return "invalid month"
if month == 2 and is_leap(year):
return 29
return month_days[month]
print(days_in_month(2017, 2))In the function: days_in_month(year, month) in the 2nd if statement how can the is_leap take in the arguement year and still be able to complete its function.
To clarify what I mean is how does the program pick up that when I assign something to the year arguement it has to run the is_leap function even though the is_leap function is coded to have the arguement of x.
If year was 12, would is_leap(year) take that 12 in and replace x with it and then run it?
2
u/CraigAT Sep 09 '21
I can see the question has been answered well. But I have a couple of minor related questions.
The first if statement, does the "not" work as intended? Personally, I would have used brackets around everything after the "not", for clarity (excluding the colon).
I was taught (in other languages) that everything needed should be passed into a function, no assumptions made. So in this case, should month_days be declared in the function, passed in as argument or 'cough' be a global variable?
2
u/biggAzzZuck Sep 09 '21
Sorry I should've mentioned, this code was from a video of Corey Schafer and I just copied it.
2
u/CraigAT Sep 09 '21
Ah. I guess he would have known the answer to #1, so it must be fine.
I'm not sure what is correct or Pythonic in those instances.
3
u/xelf Sep 09 '21 edited Sep 09 '21
I was taught (in other languages) that everything needed should be passed into a function, no assumptions made. So in this case, should month_days be declared in the function, passed in as argument or 'cough' be a global variable?
In python a function has access to (in this order):
the local variables and parameters it defines
any variables in the enclosing scope
any global variables
and any in the built-in (like True, False...)
Note that it can't modify a global variable unless you use the
globalkeyword, in general this is a bad practice and that keyword should pretty much never be used.enclosed scope is pretty much for nested functions/lambda definitions and comes up rarely.
In the case of the original example,
days_in_month()can access themonth_days[]list as it is defined in the global scope. Using a global constant like this is considered good practice. =)edit, and a link on this topic too:
https://www.datacamp.com/community/tutorials/scope-of-variables-python
3
u/CraigAT Sep 09 '21
Thank you for an excellent reply.
I wasn't aware of the read-only nature of "top/higher level" variables when used without the global keyword.
2
u/xelf Sep 09 '21 edited Sep 09 '21
The first if statement, does the "not" work as intended? Personally, I would have used brackets around everything after the "not", for clarity (excluding the colon).
Adding the parenthesis for clarity is a fine thing to do. Especially when working with developers that work on multiple languages at the same time.
They're not needed though as long as you know the order of operations, and in some cases can clutter the code so much that they remove readability rather than adding to it.
In python the last three things in order of operations for operator precedence are
not, thenand, thenor, so the comparisons are evaluated first in this case.In other words:
not 1 <= month <= 12:is
not ( 1 <= month <= 12 ):because the
<=are evaluated before the not.This has more info: https://docs.python.org/3/reference/expressions.html#operator-precedence
2
1
3
u/xelf Sep 09 '21 edited Sep 09 '21
variables do not contain data. They are merely references to a piece of data.
here we make 3 references to the list
[1,2,3]they are not different pieces of data, they are merely 3 references to the same piece of data.a,b and c are all now reference to
[0,2,3]Similarly when you pass a reference to a function, it just creates a new local reference to that piece of data. The data is what matters, not the name of the reference used to pass the data to it.
here x would be 3
here x would be passed 4 in the first call, and 10 in the second call.
This highlights the reason that the function uses a local variable -- so that you can pass different things to the same variable, but the function knows what to work with.