On the property function

koranthala koranthala at gmail.com
Tue Jun 16 05:16:54 EDT 2009


On Jun 16, 9:19 am, Chris Rebert <c... at rebertia.com> wrote:
> On Mon, Jun 15, 2009 at 5:43 AM, Virgil Stokes<v... at it.uu.se> wrote:
> > Does anyone have a good example (or examples) of when "property(...)" can be
> > useful?
>
> Erm, when you want to create a property (i.e. computed attribute).
>
> from __future__ import division
> class TimeDelta(object):
>     def __init__(self, secs):
>         self. seconds =secs
>
>     @property
>     def minutes(self):
>         return self.seconds/60
>
>     @property
>     def hours(self):
>         return self.minutes/60
>
>     @property
>     def days(self):
>         return self.hours/24
>
> td = TimeDelta(55555555555)
> print td.days, "days", "=", td.hours, "hours", "=", td.minutes, "minutes"
>
> Cheers,
> Chris
> --http://blog.rebertia.com

To add a little more, this link _ http://dirtsimple.org/2004/12/python-is-not-java.html
_ might help. - Search for 'Getters and setters' and you will reach
the place where property is explained more.



More information about the Python-list mailing list