Re: Are the property Function really useful? yes.

Discussion in 'Python' started by Dave Angel, Aug 30, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 08/29/2012 07:46 AM, Dave Angel wrote:
    > On 08/29/2012 06:32 AM, 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>&nbsp;</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
    Dave Angel, Aug 30, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hattuari
    Replies:
    11
    Views:
    821
    Julie J.
    Feb 26, 2004
  2. teo
    Replies:
    3
    Views:
    513
  3. Jamie Herre
    Replies:
    1
    Views:
    174
    why the lucky stiff
    Jan 7, 2005
  4. Mark Lawrence
    Replies:
    0
    Views:
    177
    Mark Lawrence
    Aug 29, 2012
  5. Dieter Maurer
    Replies:
    0
    Views:
    175
    Dieter Maurer
    Aug 30, 2012
Loading...

Share This Page