[英]Is it a right practice to place more than one function with return inside the Python class definition?
Here is the code for finding the area of triangle, in which i have initialized the dimensions of triangle in the def __init__
and also i need its area to be initialized for these initial dimensions, for this i have placed self.a1
in the def __init__
also.这是查找三角形面积的代码,其中我已经在
def __init__
中初始化了三角形的尺寸,并且我还需要为这些初始尺寸初始化它的面积,为此我将self.a1
放在def __init__
中还。 A Function def tarea()
is returning the value to self.a1
in the __init__
function. Function
def tarea()
将值返回给__init__
self.a1
中的 self.a1。 An another function def getarea()
is inside the class definition for returning the area of triangle, when the dimensions of triangle is set.另一个 function
def getarea()
在 class 定义内,用于在设置三角形尺寸时返回三角形面积。 Just need to know if this task can be implemented in more efficient manner?只需要知道是否可以以更有效的方式执行此任务? To write two functions with return inside a class definition is ok?
在 class 定义中编写两个带返回的函数可以吗?
class triangle:
def __init__(self, sa1, sa2, sa3 ):
self.s1 = sa1
self.s2 = sa2
self.s3 = sa3
self.a1 = self.tarea()
def sets1(self, ss1):
self.s1 = ss1
def sets2(self, ss2):
self.s2 = ss2
def sets3(self, ss3):
self.s3 = ss3
def tarea(self):
a = (self.s1 + self.s2 +self.s3)/2
return (a*(a - self.s1)*(a-self.s2)*(a-self.s3))**0.5
def getarea(self):
self. a1 = self.tarea()
return self.a1
tri = triangle(3,4,5)
print("Area of triangle=", tri.a1)
tri.sets1(4)
tri.sets2(5)
tri.sets3(6)
print("Area of triangle with another values",f'{tri.getarea():.2f}')
I would define the area as a property
that computes the area based on the current sides.我会将区域定义为基于当前边计算区域的
property
。
class Triangle:
def __init__(self, sa1, sa2, sa3 ):
self.s1 = sa1
self.s2 = sa2
self.s3 = sa3
@property
def area(self):
a = (self.s1 + self.s2 +self.s3)/2
return (a*(a - self.s1)*(a-self.s2)*(a-self.s3))**0.5
If you consider the area to expensive to calculate on-demand each if the sides haven't changed, you can cache the area.如果您认为在边没有改变的情况下按需计算该区域的成本很高,则可以缓存该区域。 We make each of the sides a property that can invalidate the cache when a side changes.
我们让每一边都成为一个属性,当一个边发生变化时,它可以使缓存失效。
The boilerplate-heavy version:样板繁重的版本:
class Triangle:
def __init__(self, sa1, sa2, sa3 ):
self._s1 = sa1
self._s2 = sa2
self._s3 = sa3
self._area = None
# We could also use the setters to set self.s1 = sa1,
# etc, and let them initialize self._area to None.
@property
def s1(self):
return self._s1
@s1.setter
def s1(self, value):
self._s1 = value
self._area = None
@property
def s2(self):
return self._s2
@s2.setter
def s2(self, value):
self._s2 = value
self._area = None
@property
def s3(self):
return self._s3
@s3.setter
def s3(self, value):
self._s3 = value
self._area = None
@property
def area(self):
if self._area is None:
self._area = self._compute_area()
return self._area
def _compute_area(self):
a = (self.s1 + self.s2 +self.s3)/2
return (a*(a - self.s1)*(a-self.s2)*(a-self.s3))**0.5
A lighter version, without getting into a full-blown descriptor implementation.一个更轻的版本,没有进入一个成熟的描述符实现。
def make_side_property(name):
def get(self):
return getattr(self, name)
def set(self, value):
setattr(self, name, value)
self._area = None
return property(get, set)
class Triangle:
def __init__(self, sa1, sa2, sa3 ):
self.s1 = sa1
self.s2 = sa2
self.s3 = sa3
# Here we do let the setters initialize self._area
s1 = make_side_property("_s1")
s2 = make_side_property("_s2")
s3 = make_side_property("_s3")
@property
def area(self):
if self._area is None:
self._area = self._compute_area()
return self._area
def _compute_area(self):
a = (self.s1 + self.s2 +self.s3)/2
return (a*(a - self.s1)*(a-self.s2)*(a-self.s3))**0.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.