khanna-lab / cadre

Other
0 stars 0 forks source link

Recording timing of events: Accessing variable outside a class #11

Closed khanna7 closed 2 years ago

khanna7 commented 2 years ago

https://github.com/khanna7/cadre/blob/e0ba698031de6e6186bb40ebc56e9a1ed3c18c42/python/src/simple-alc-use-transitions-oop.py#L140

@dsheeler: I want to access the time variable (which moves us through the loop) in my simulate_incarceration function, so I can record the time that an agent is incarcerated. I am thinking I can add an incarceration_time attribute for each person and set it equal to the time when they are incarcerated. Any ideas on how I would I do that? (Or any alternate implementations?)

khanna7 commented 2 years ago

Initialize current time:

class Person():
    def __init__(self, 
    name=None, 
    age=None, 
    race=None, 
    female=None,
    smoker=None, 
    alc_use_status=None,
    current_incarceration_status = None,
    current_incarceration_time = -1): #parameter to person class

        self.name = name    
        self.age = age
        self.race = race
        self.female = female
        self.smoker = smoker
        self.alc_use_status = alc_use_status
        self.current_incarceration_status = current_incarceration_status,
        self.current_incarceration_time = -1 #field in person class

 ## if a new person is constructed with only the default value of a parameter, it only needs to be in the field, 
## argument in the constructor is not needed.

....

self.my_persons.append(person)
            age_sum = person.age + age_sum
            race.append(person.race) 
            females = person.female + females 
            alc_use_status.append(person.alc_use_status)
            smokers = person.smoker + smokers
           current_incarceration_time = person.current_incarceration_time
khanna7 commented 2 years ago

"settr" methods can be used to set the attributes to hide fields in some way. Only makes sense if we are not just returning the value.