AK-CHP
AK-CHP

Reputation: 101

import behaviour in python

I have a doubt regarding import statement in python. Here is my functions.py file

week_days = ["Sunday", "Monday", "Tuesday",
             "Wednesday", "Thursday", "Friday", "Saturday"]


def get_week_day(day_no: int):
    return week_days[day_no % 7]

and testing .py:

from functions import get_week_day

print(get_week_day(100))

And the output is Tuesday. In my testing.py file I am only importing get_week_day function from functions.py but still the function is able to access the array week_days. How...?

Please explain this.

Upvotes: 1

Views: 50

Answers (1)

user18198735
user18198735

Reputation:

as you can read on this answer from functions import get_week_day still imports the whole Module. So it seems logic that since def get_week_day(day_no: int): refers to week_days, it accesses week_days from the imported module. Reading documentation might solve any further doubts on this matter.

Upvotes: 2

Related Questions