python中物理量的命名

发布于 2024-10-03 05:41:08 字数 1679 浏览 2 评论 0原文

我想为我的模拟代码中使用的物理/数学量建立一个良好的命名方案。考虑以下示例:

from math import *

class GaussianBeamIntensity(object):
    """
    Optical intensity profile of a Gaussian laser beam.
    """

    def __init__(self, intensity_at_waist_center, waist_radius, wavelength):
        """
        Arguments:

        *intensity_at_waist_center*: The optical intensity of the beam at the
            center of its waist in W/m^2 units.

        *waist_radius*: The radius of the beam waist in meters.

        *wavelength*: The wavelength of the laser beam in meters.

        """

        self.intensity_at_waist_center = intensity_at_waist_center
        self.waist_radius = waist_radius
        self.wavelength = wavelength
        self._calculate_auxiliary_quantities()

    def _calculate_auxiliary_quantities(self):
        # Shorthand notation
        w_0, lambda_ = self.waist_radius, self.wavelength

        self.rayleigh_range = pi * w_0**2 / lambda_
        # Generally some more quantities could follow

    def __call__(self, rho, z):
        """
        Arguments:

        *rho*, *z*: Cylindrical coordinates of a spatial point.
        """
        # Shorthand notation
        I_0, w_0 = self.intensity_at_waist_center, self.waist_radius
        z_R = self.rayleigh_range

        w_z = w_0 * sqrt(1.0 + (z / z_R)**2)
        I = I_0 * (w_0 / w_z)**2 * exp(-2.0 * rho**2 / w_z**2)
        return I

为了在可读性简洁符号(公式保持相对较短)?您能改进一下上面的例子吗?或者提出更好的方案?

最好遵循 PEP8 的准则,记住“愚蠢的一致性是小头脑的恶魔”。在遵守传统的 80 个字符的行长度限制的同时,坚持使用描述性名称似乎很困难。

先感谢您!

I would like to establish a good naming scheme for physical/mathematical quantities used in my simulation code. Consider the following example:

from math import *

class GaussianBeamIntensity(object):
    """
    Optical intensity profile of a Gaussian laser beam.
    """

    def __init__(self, intensity_at_waist_center, waist_radius, wavelength):
        """
        Arguments:

        *intensity_at_waist_center*: The optical intensity of the beam at the
            center of its waist in W/m^2 units.

        *waist_radius*: The radius of the beam waist in meters.

        *wavelength*: The wavelength of the laser beam in meters.

        """

        self.intensity_at_waist_center = intensity_at_waist_center
        self.waist_radius = waist_radius
        self.wavelength = wavelength
        self._calculate_auxiliary_quantities()

    def _calculate_auxiliary_quantities(self):
        # Shorthand notation
        w_0, lambda_ = self.waist_radius, self.wavelength

        self.rayleigh_range = pi * w_0**2 / lambda_
        # Generally some more quantities could follow

    def __call__(self, rho, z):
        """
        Arguments:

        *rho*, *z*: Cylindrical coordinates of a spatial point.
        """
        # Shorthand notation
        I_0, w_0 = self.intensity_at_waist_center, self.waist_radius
        z_R = self.rayleigh_range

        w_z = w_0 * sqrt(1.0 + (z / z_R)**2)
        I = I_0 * (w_0 / w_z)**2 * exp(-2.0 * rho**2 / w_z**2)
        return I

What consistent naming scheme would you propose for the physical properties (properties, function arguments etc.) in order to balance between readability and concise notation (that formulae remain relatively short)? Could you please refine the example above? Or perhaps propose a better scheme?

It would be nice to follow the guidelines of PEP8, remembering that "A Foolish Consistency is the Hobgoblin of Little Minds". It seems difficult to stick to descriptive names while obeying the traditional 80-character limit for line lengths.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-10-10 05:41:08

我想你已经找到了良好的平衡点。富有表现力的名称很重要,因此我完全同意使用 Wavelenght 而不是 lambda 作为类属性。这样界面就保持清晰且富有表现力。

不过,在长公式中,lambda_ 是一个不错的选择,因为这是光学中普遍接受且广泛使用的波长表示法。我认为当你实现一个公式时,你想要做的就是尽可能接近你写在纸上的方程的形式(或者它们出现在文章等中)。

简而言之:保持界面富有表现力,公式简短。

I think you've already found the good balance. Expressive names are important, so I totally agree with the use of wavelenght instead of lambda as a class attribute. This way the interface remains clear and expressive.

In a long formula, though, lambda_ is good choice as shorthand notation, because this is a commonly accepted and widely used notation for the wavelength in optics. I think when you implement a formula, what you want to do is staying as close as possible to the form of the equations you'd write on a piece of paper (or as they appear in an article etc).

In short: keep the interfaces expressive, the formulae short.

可爱暴击 2024-10-10 05:41:08

使用Python3,您可以使用实际符号 λ 作为变量名。

我期待编写如下代码:

from math import pi as π

sphere_volume = lambda r : 4/3 * π * r**3

Use Python3 and you can use the actual symbol λ for a variable name.

I look forward to writing code like:

from math import pi as π

sphere_volume = lambda r : 4/3 * π * r**3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文