Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:09:11+00:00 2026-06-10T23:09:11+00:00

Possible Duplicate: python: are property fields being cached automatically? As a concern about effeciency

  • 0

Possible Duplicate:
python: are property fields being cached automatically?

As a concern about effeciency of properties in python, I was wondering when and how often they are called.

To use a simple example, say I subclass namedtuple and I have something like:

from collections import namedtuple
from math import pi

class Circle (namedtuple('Circle', 'x, y, r')):
    __slots__ = ()

    @property
    def area(self):
        return pi*self.r**2

unitCircle = Circle(0, 0, 1.0)
print 'The area of the unit circle is {0} units'.format(unitCircle.area)

I assume that area is not calculated until the first time it is called, but once it is called, is that value cached until something changes or is it recalculated every time it is called?

Put another way, if I have a property that (unlike this one) is relatively expensive to calculate and will be used repeatedly, should I let it be a property, or is it more effecient to store it as a value and explicitly caclulate it when it really needs to be updated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:09:13+00:00Added an answer on June 10, 2026 at 11:09 pm

    Properties are not cached unless you do it explicitly, so the code is run every time the property is accessed. Try:

    @property
    def area(self):
        try:
            return self._area
        except AttributeError:
            area = pi*self.r**2
            self._area = area
            return area
    

    If you want to be able to recalculate the value on demand occasionally, do something like:

    @property
    def area(self):
        try:
            return self._area
        except AttributeError:
            self.recalc_area()
            return self._area
    
    def recalc_area(self):
        self._area = pi*self.r**2
    

    Or, if you want to do it more automatically:

    @property
    def area(self):
        try:
            return self._area
        except AttributeError:
            area = pi*self.radius**2
            self._area = area
            return area
    
    @property
    def radius(self):
        return self._radius
    
    @radius.setter
    def radius(self, radius):
        try:
            del self._area
        except AttributeError:
            pass
        self._radius = radius
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Real world example about how to use property feature in python? I
Possible Duplicate: Python timer countdown Hi guys, I want to know about timer in
Possible Duplicate: Python list confusion I've got one little question about Python lists: Why
Possible Duplicate: Can set any property of Python object In [67]: obj = object()
Possible Duplicate: Dynamic loading of python modules python: How to add property to a
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: Python: Behaviour of increment and decrement operators >>> a=2 >>> ++a 2
Possible Duplicate: python: how to jump to a particular line in a huge text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.