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 7751587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:35:13+00:00 2026-06-01T11:35:13+00:00

This is a design principle question for classes dealing with mathematical/physical equations where the

  • 0

This is a design principle question for classes dealing with mathematical/physical equations where the user is allowed to set any parameter upon which the remaining are being calculated.
In this example I would like to be able to let the frequency be set as well while avoiding circular dependencies.

For example:

from traits.api import HasTraits, Float, Property
from scipy.constants import c, h
class Photon(HasTraits):
    wavelength = Float # would like to do Property, but that would be circular?
    frequency = Property(depends_on = 'wavelength')
    energy = Property(depends_on = ['wavelength, frequency'])
    def _get_frequency(self):
        return c/self.wavelength
    def _get_energy(self):
        return h*self.frequency

I’m also aware of an update trigger timing problem here, because I don’t know the sequence the updates will be triggered:

  1. Wavelength is being changed
  2. That triggers an updated of both dependent entities: frequency and energy
  3. But energy needs frequency to be updated so that energy has the value fitting to the new wavelength!

(The answer to be accepted should also address this potential timing problem.)

So, what’ the best design pattern to get around these inter-dependent problems?
At the end I want the user to be able to update either wavelength or frequency and frequency/wavelength and energy shall be updated accordingly.

This kind of problems of course do arise in basically all classes that try to deal with equations.

Let the competition begin! 😉

  • 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-01T11:35:14+00:00Added an answer on June 1, 2026 at 11:35 am

    Thanks to Adam Hughes and Warren Weckesser from the Enthought mailing list I realized what I was missing in my understanding.
    Properties do not really exist as an attribute. I now look at them as something like a ‘virtual’ attribute that completely depends on what the writer of the class does at the time a _getter or _setter is called.

    So when I would like to be able to set wavelength AND frequency by the user, I only need to understand that frequency itself does not exist as an attribute and that instead at _setting time of the frequency I need to update the ‘fundamental’ attribute wavelength, so that the next time the frequency is required, it is calculated again with the new wavelength!

    I also need to thank the user sr2222 who made me think about the missing caching. I realized that the dependencies I set up by using the keyword ‘depends_on’ are only required when using the ‘cached_property’ Trait. If the cost of calculation is not that high or it’s not executed that often, the _getters and _setters take care of everything that one needs and one does not need to use the ‘depends_on’ keyword.

    Here now the streamlined solution I was looking for, that allows me to set either wavelength or frequency without circular loops:

    class Photon(HasTraits):
        wavelength = Float 
        frequency = Property
        energy = Property
    
        def _wavelength_default(self):
            return 1.0
        def _get_frequency(self):
            return c/self.wavelength
        def _set_frequency(self, freq):
            self.wavelength = c/freq
        def _get_energy(self):
            return h*self.frequency
    

    One would use this class like this:

    photon = Photon(wavelength = 1064)
    

    or

    photon = Photon(frequency = 300e6)
    

    to set the initial values and to get the energy now, one just uses it directly:

    print(photon.energy)
    

    Please note that the _wavelength_default method takes care of the case when the user initializes the Photon instance without providing an initial value. Only for the first access of wavelength this method will be used to determine it. If I would not do this, the first access of frequency would result in a 1/0 calculation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this pimpl design where the implementation classes are polymorphic but the interfaces
Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I'm
I had a question about a Binary Sorted Tree design principle. I need to
Update: Please read this question in the context of design principles, elegance, expression of
I've seen this question: Class design with vector as a private/public member? , but
This design question needs a bit of context, so please bear with me. I
Got this design issue with JSF + Spring :- I got a datatable on
I keep running into this design problem, and I'm not happy with my solution
I need to make this design: http://www.stephburningham.com/lmg/ into a joomla template but I've never
I have this design which I cannot seem to get right, I would like

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.