1

I have a variable which is a list, which contains a large number of people in it. something like:

'citizens = ['John', 'Jack', 'Jill', 'Jonas']`
#imagine hundreds of names

I would like to 'link' a variable, population to the length of this list, so that whenever the list is modified, the population variable will update. Up until now, I have been simply calling a function like so:

def update_population()
    global population
    population = len(citizens)

but is seems like there would be a more elegant solution, so this can be automatically triggered without my explicitly calling it.

1
  • You want a class that has a citizens field and a population method decorated with @property. Commented Dec 7, 2015 at 2:13

1 Answer 1

4

If this is in a class somewhere, then you could make a it a property, e.g.

@property
def population(self):
    return len(self.citizens)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.