在 Python 中初始化一个对象 class 时是否应该有一个函数 运行?
Should you have a function run when you initialize an object class in Python?
我有一个正在处理的项目,我有一个 Python Class 对象可以正常工作,但我很好奇当我调用 __init__
方法。
现在,我有一个函数 (demand_curve
),它会在您创建 class 对象的新实例时 运行。 如果我在初始化 class 时 运行 一个函数可以吗?还是会让我在以后遇到问题?
完整代码供您参考:
# initalize the lists
prices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
quantities = [13.75, 12.5, 11.25, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25, 0.0]
class MarketFunction(object):
def __init__(self, prices, quantities):
self.prices = prices
self.quantities = quantities
self.intercept = None
self.slope = None
# Run the demand_curve function to calculate the slope and the intercept.
self.demand_curve(prices, quantities)
@property
def DemandIntercept(self):
'''
SUMMARY: Return the intercept of the demand curve.
RETURN TYPE: float
'''
if self.intercept is None:
intercept = self.demand_curve(self.prices, self.quantities)
self.intercept = intercept[0]
return self.intercept
@property
def MaxPrice(self):
'''
SUMMARY: Return the maximum price from the Prices list.
RETURN TYPE: float
'''
return max(self.prices)
@property
def MinPrice(self):
'''
SUMMARY: Return the minimum price from the Prices list.
RETURN TYPE: float
'''
return min(self.prices)
@property
def MaxQuantity(self):
'''
Summary: Return the maximum quantity from the Quantities list.
RETURN TYPE: int
'''
return max(self.prices)
@property
def MinQuantity(self):
'''
SUMMARY: Return the minimum quantity from the Quantities list.
RETURN TYPE: int
'''
return min(self.quantities)
def demand_curve(self, prices, quantities):
'''
SUMMARY: Given an array of prices and quantities calculate the slope and Y-intercept of the line and set the
values for the MarketFunction object.
PARA prices: A list of prices
PARA TYPE: list
PARA quantites: A list of quantites
PARA TYPE: list
'''
# grab the first few values.
x0, x1 = prices[0], prices[1]
y0, y1 = quantities[0], quantities[1]
# calculate the metrics
slope = (y1 - y0)/(x1 - x0)
y_int = (-slope * x0) + y0
self.intercept = y_int
self.slope = slope
# create a new instance of the object
market = MarketFunction(prices = prices, quantities = quantities)
# Grab the DemandIntercept
demand_function = market.DemandIntercept
demand_function
使用__init__
里面的函数或方法是绝对没有问题的。对象已经分配,__init__
方法只是这样做,初始化对象,所以它是完全安全的。
我有一个正在处理的项目,我有一个 Python Class 对象可以正常工作,但我很好奇当我调用 __init__
方法。
现在,我有一个函数 (demand_curve
),它会在您创建 class 对象的新实例时 运行。 如果我在初始化 class 时 运行 一个函数可以吗?还是会让我在以后遇到问题?
完整代码供您参考:
# initalize the lists
prices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
quantities = [13.75, 12.5, 11.25, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25, 0.0]
class MarketFunction(object):
def __init__(self, prices, quantities):
self.prices = prices
self.quantities = quantities
self.intercept = None
self.slope = None
# Run the demand_curve function to calculate the slope and the intercept.
self.demand_curve(prices, quantities)
@property
def DemandIntercept(self):
'''
SUMMARY: Return the intercept of the demand curve.
RETURN TYPE: float
'''
if self.intercept is None:
intercept = self.demand_curve(self.prices, self.quantities)
self.intercept = intercept[0]
return self.intercept
@property
def MaxPrice(self):
'''
SUMMARY: Return the maximum price from the Prices list.
RETURN TYPE: float
'''
return max(self.prices)
@property
def MinPrice(self):
'''
SUMMARY: Return the minimum price from the Prices list.
RETURN TYPE: float
'''
return min(self.prices)
@property
def MaxQuantity(self):
'''
Summary: Return the maximum quantity from the Quantities list.
RETURN TYPE: int
'''
return max(self.prices)
@property
def MinQuantity(self):
'''
SUMMARY: Return the minimum quantity from the Quantities list.
RETURN TYPE: int
'''
return min(self.quantities)
def demand_curve(self, prices, quantities):
'''
SUMMARY: Given an array of prices and quantities calculate the slope and Y-intercept of the line and set the
values for the MarketFunction object.
PARA prices: A list of prices
PARA TYPE: list
PARA quantites: A list of quantites
PARA TYPE: list
'''
# grab the first few values.
x0, x1 = prices[0], prices[1]
y0, y1 = quantities[0], quantities[1]
# calculate the metrics
slope = (y1 - y0)/(x1 - x0)
y_int = (-slope * x0) + y0
self.intercept = y_int
self.slope = slope
# create a new instance of the object
market = MarketFunction(prices = prices, quantities = quantities)
# Grab the DemandIntercept
demand_function = market.DemandIntercept
demand_function
使用__init__
里面的函数或方法是绝对没有问题的。对象已经分配,__init__
方法只是这样做,初始化对象,所以它是完全安全的。