How to avoid circular dependencies when setting Properties?

前端 未结 2 1947
清酒与你
清酒与你 2021-01-21 09:12

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 ca

2条回答
  •  佛祖请我去吃肉
    2021-01-21 09:48

    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.

提交回复
热议问题