Are the property Function really useful? yes.

Dave Angel d at davea.name
Thu Aug 30 07:49:10 EDT 2012


On 08/29/2012 07:46 AM, Dave Angel wrote:
> On 08/29/2012 06:32 AM, levinie001 at gmail.com wrote:
>
>

I was trying to point out that your question was empty (no content in
the message).  Mark also apparently saw an empty message. However, now
that Dieter has responded, with apparent quotes from your message, i see
what the problem was.

You composed an html message and sent it to a text forum.  Many people
make that mistake, and the problem it usually causes is that source code
indentation is messed up.  However, most mail programs send a text part
as well, and that's what we see.   In your case, the text part was
empty, so I saw nothing but the subject line.

Please tell your mail program to compose TEXT message, not html, or this
problem could occur again.


Now to your question.

>>  <div> </div>
>>  <div>Where can i use the property function?</div></div></body></html>

In some languages, you get computed properties by writing getter and
setter functions, with names to match.  Python lets you hide the fact
that the "property" is hidden, by letting you use ordinary syntax to
access it.  For example, suppose you had a Point class, which stored the
x and y coordinates of a point on a plane.  Suppose also that sometimes
you wanted to use polar coordinates.  You might like the user of the
class to just interchangeably use the real attributes and the computed
ones, without having to use parentheses on some of them.  (untested)

class Point(object):
     def __init__(self, x, y):
           self.x = x
           self.y = y
     @property
     def  radius(self):
         return   self.sqrt(self.x * self.x, self.y * self.y)
     @property
     def  theta(self):
          return math.atan(self.x, self.y)

Now, once you have a Point instance, you can use all four attributes pretty much interchangeably.

-- 

DaveA




More information about the Python-list mailing list