Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Special Methods

Jay Norris
Jay Norris
14,824 Points

Alternative way to set property?

I've previously set a property in a Django model class using property() and it also works.

I'm wondering if it's possibly an outdated way of setting the property and I should re-factor using @property? The code works great, but I was just curious about the difference. Here is the relevant bit:

class Person(models.Model): 
    gender = models.CharField(max_length=8, choices=GENDERS, default='')
    birthdate = models.DateField(max_length=25)
    tobacco = models.CharField(max_length=5, choices=YES_NO, default='NO')
    household = models.ForeignKey(Household)

    def _age(self):
       """
       Returns the age from the date of birth
       """
       now = datetime.datetime.now()
       return now.year - self.birthdate.year - ((now.month, now.day) < (self.birthdate.month, self.birthdate.day))
    age = property(_age)