python中物理量的命名
我想为我的模拟代码中使用的物理/数学量建立一个良好的命名方案。考虑以下示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你已经找到了良好的平衡点。富有表现力的名称很重要,因此我完全同意使用 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.
使用Python3,您可以使用实际符号 λ 作为变量名。
我期待编写如下代码:
Use Python3 and you can use the actual symbol λ for a variable name.
I look forward to writing code like: