小编are*_*192的帖子

在 Python 中初始化对象类时是否应该运行一个函数?

我正在处理一个项目,并且有一个运行良好的 Python 类对象,但我很好奇当我调用该__init__方法时它是否设计正确。

现在,我有一个函数 ( demand_curve),当您创建类对象的新实例时,它将运行。如果我在初始化类时运行一个函数可以吗?还是会让我在以后遇到问题?

以下是完整代码供您参考:

# 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: …
Run Code Online (Sandbox Code Playgroud)

python initialization class

6
推荐指数
1
解决办法
5003
查看次数

标签 统计

class ×1

initialization ×1

python ×1