Python OOp, updating some instance attributes when class method is called

Hi, I am new to this forum. I hope I get some help with my problem.

I am facing a problem updating the instance age.
I have highlighted the problem in the image.

I would really appreciate any suggestions.

Thank you

Hello, @sangam. I made some changes at your code and hope those changes will help you:

from datetime import date
class Migration:
    humanInstances = []#Dicts destroy-I mean, cut off- repeating keys(and those keys' values). So,I think, using a list is better than using a dict -which uses names as
    #keys- because names may repeat.
    human_population = 100
    current_year = date.today().year
    addyears = 0
    
    def __init__(self,name,gender,age):
        self.name = name
        self.lifephase = "Not Available"
        self.age = age
        self.gender = gender
        Migration.human_population += 1
        Migration.humanInstances.append(self)#This will add your human to humanInstances list.

    @classmethod
    def time_passedby(Migration,years):
        Migration.current_year += years
        Migration.addyears = years
        #This for loop will update your instances' ages and lifephases:
        for human in Migration.humanInstances:
            human.age += years
            human.lifephase = human.lifePhase()

    def lifePhase(self):
        #bla bla
        #bla bla

Note: Please share your code as text in your future posts. You can use the </> button in your editor, or you can easily create the formation below:

```
Your code goes here

```

2 Likes

I am mildly concerned this code calls the ‘lifephase’ after 100 years ‘terminated’ :slight_smile:

A good point.:clap: Computer science is too dangerous nowadays…:smile:

Thanks, Toprak. I really, appreciated the solution. :pray:

1 Like