Jan-04-2017, 11:32 PM
Hi,
I have the variable sun_Datetime that was initialized globally outside of 2 user defined functions that I have: 1) get_previous_byday(dayname, start_date=None), 2) date_to_str(day, month, year, sun_dtime).
I tried to access the var sun_Datetime inside date_to_str function and change it.
When sun_Datetime initialized outside of date_to_str function, the variable object id is 54219768. Once within date_to_str function, after calling get_previous_byday function to change the sun_Datetime variable, a new object was created with an different id, 37199768 instead.
I declared sun_Datetime as a global variable, doesn't this allow me to access the original variable, id 54219768 and make changes to it?
I have the variable sun_Datetime that was initialized globally outside of 2 user defined functions that I have: 1) get_previous_byday(dayname, start_date=None), 2) date_to_str(day, month, year, sun_dtime).
I tried to access the var sun_Datetime inside date_to_str function and change it.
When sun_Datetime initialized outside of date_to_str function, the variable object id is 54219768. Once within date_to_str function, after calling get_previous_byday function to change the sun_Datetime variable, a new object was created with an different id, 37199768 instead.
I declared sun_Datetime as a global variable, doesn't this allow me to access the original variable, id 54219768 and make changes to it?
import datetime from datetime import datetime, timedelta import time weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] def get_previous_byday(dayname, start_date=None): if start_date is None: start_date = datetime.today() day_num = start_date.weekday() day_num_target = weekdays.index(dayname) days_ago = (7 + day_num - day_num_target) % 7 if days_ago == 0: days_ago = 7 target_date = start_date - timedelta(days=days_ago) return target_date def date_to_str(day, month, year, sun_dtime): global sun_Datetime global sun_Day global sun_Month global sun_Year print("INSIDE date_to_str() function BEFORE get_previous_byday() function call") print("variable: [value, type, id] -->", "sun_Datetime passed as sun_dtime: ", [sun_dtime, type(sun_dtime), id(sun_dtime)], "\n") sun_dtime = get_previous_byday('Sunday', sun_dtime) ## reference line 4 print("INSIDE date_to_str() function AFTER get_previous_byday() function call") print("variable: [value, type, id] -->", "sun_Datetime passed as sun_dtime: ", [sun_dtime, type(sun_dtime), id(sun_dtime)], "\n") day = sun_dtime.day ## reference line 5 month = sun_dtime.month ## reference line 6 year = sun_dtime.year ## reference line 7 sun_Day = str(day) sun_Year = str(year) if len(str(month)) == 1: sun_Month = '0' + str(month) else: sun_Month = str(month) sun_Datetime = get_previous_byday('Sunday') sun_Day = str(sun_Datetime.day) ## reference line 1 sun_Month = str(sun_Datetime.month) ## reference line 2 sun_Year = str(sun_Datetime.year) ## reference line 3 print("BEFORE calling date_to_str() function") print("variable: [value, type, id] -->", "sun_Datetime: ", [sun_Datetime, type(sun_Datetime), id(sun_Datetime)], "\n") print("These variables were initialized OUTSIDE of date_to_str() function on reference line 1, 2, 3") print("variable: [value, type, id] -->", "sun_Day: ", [sun_Day, type(sun_Day), id(sun_Day)]) print("variable: [value, type, id] -->", "sun_Month: ", [sun_Month, type(sun_Month), id(sun_Month)]) print("variable: [value, type, id] -->", "sun_Year: ", [sun_Year, type(sun_Year), id(sun_Year)], "\n") date_to_str(sun_Datetime.day, sun_Datetime.month, sun_Datetime.year, sun_Datetime) print("Tried to change the following variables INSIDE date_to_str() function on reference line 4,5,6 and 7") print("AFTER calling date_to_str() function") print("variable: [value, type, id] -->", "sun_Datetime: ", [sun_Datetime, type(sun_Datetime), id(sun_Datetime)], "\n") print("variable: [value, type, id] -->", "sun_Day: ", [sun_Day, type(sun_Day), id(sun_Day)]) print("variable: [value, type, id] -->", "sun_Month: ", [sun_Month, type(sun_Month), id(sun_Month)]) print("variable: [value, type, id] -->", "sun_Year: ", [sun_Year, type(sun_Year), id(sun_Year)], "\n")
Output:BEFORE calling date_to_str() function
variable: [value, type, id] --> sun_Datetime: [datetime.datetime(2017, 1, 1, 18, 22, 0, 862455), <class 'datetime.datetime'>, 54219768]
These variables were initialized OUTSIDE of date_to_str() function on reference line 1, 2, 3
variable: [value, type, id] --> sun_Day: ['1', <class 'str'>, 37478176]
variable: [value, type, id] --> sun_Month: ['1', <class 'str'>, 37732000]
variable: [value, type, id] --> sun_Year: ['2017', <class 'str'>, 37731968]
INSIDE date_to_str() function BEFORE get_previous_byday() function call
variable: [value, type, id] --> sun_Datetime passed as sun_dtime: [datetime.datetime(2017, 1, 1, 18, 22, 0, 862455), <class 'datetime.datetime'>, 54219768]
INSIDE date_to_str() function AFTER get_previous_byday() function call
variable: [value, type, id] --> sun_Datetime passed as sun_dtime: [datetime.datetime(2016, 12, 25, 18, 22, 0, 862455), <class 'datetime.datetime'>, 37199768]
Tried to change the following variables INSIDE date_to_str() function on reference line 4,5,6 and 7
AFTER calling date_to_str() function
variable: [value, type, id] --> sun_Datetime: [datetime.datetime(2017, 1, 1, 18, 22, 0, 862455), <class 'datetime.datetime'>, 54219768]
variable: [value, type, id] --> sun_Day: ['25', <class 'str'>, 51992256]
variable: [value, type, id] --> sun_Month: ['12', <class 'str'>, 37731968]
variable: [value, type, id] --> sun_Year: ['2016', <class 'str'>, 37478176]