יצא שבהרבה פונקציות הייתי צריך את הpath של תיקיית הwit שנמצאת בcwd.
אני יודע שלא מומלץ להגדיר בתוך פונקציה משתנה גלובלי ואז להשתמש באותו משתנה בפונקציות אחרות.
השאלה היא אם זה נכון לכל סוג משתנה? והאם יש כמות חזרות של קוד שכבר נחשבת למוגזמת והיא מצדיקה שימוש במשתנה גלובלי?
קוד לדוגמה:
def a():
wit_path = find_main_dir() / ‘.wit’
…def b():
wit_path = find_cwd() / ‘.wit’
do_something(wit_path)
…def c():
wit_path = find_cwd() / ‘.wit’
do_something(wit_path)
…def d():
wit_path = find_cwd() / ‘.wit’
do_something(wit_path)
…
לעומת:
def a():
global wit_path = find_main_dir() / ‘.wit’
…def b():
do_something(wit_path)
…def c():
do_something(wit_path)
…def d():
do_something(wit_path)
…