Python 来自def的呼叫列表

Python 来自def的呼叫列表,python,Python,我有这样的定义: def get_price_table(): pricing_table = [3.60, 2.90, 2.20, 2.10, 1.70, 1.30] #converting the list objects into floats with two decimals pricing_table = [ '%.2f' % elem for elem in pricing_table] return prici

我有这样的定义:

def get_price_table():

        pricing_table = [3.60, 2.90, 2.20, 2.10, 1.70, 1.30]

        #converting the list objects into floats with two decimals
        pricing_table = [ '%.2f' % elem for elem in pricing_table]

        return pricing_table
现在我想在下面的课程中调用它:

class CalcPrice():

    def compute_price(self, info: UIInfo) -> float:
        # get number of tariefeenheden
        tariefeenheden: int = Tariefeenheden.get_tariefeenheden(info.from_station, info.to_station)

        PricingTable.get_price_table()

        if info.travel_class == UIClass.FirstClass:
            if info.discount == UIDiscount.TwentyDiscount:
                price = PricingTable.get_price_table.pricing_table[1]
            elif info.discount == UIDiscount.FortyDiscount:
                price = PricingTable.get_price_table.pricing_table[2]
            else:
                price = PricingTable.get_price_table.pricing_table[0]

        elif info.travel_class == UIClass.SecondClass:
            if info.discount == UIDiscount.TwentyDiscount:
                price = PricingTable.get_price_table.pricing_table[4]
            elif info.discount == UIDiscount.FortyDiscount:
                price = PricingTable.get_price_table.pricing_table[5]
            else:
                price = PricingTable.get_price_table.pricing_table[3]

        #Double price if returnticket is applicable
        if info.way == UIWay.Return:
            price *= 2

        price = price * 0.02 * tariefeenheden
        return round(price, 2)

但现在我得到一个AttributeError,它说'function'对象没有'pricing_table'属性。从另一个def获取列表元素的正确方法是什么?

也许您是有意这样做的

class CalcPrice():

    def compute_price(self, info: UIInfo) -> float:
        # get number of tariefeenheden
        tariefeenheden: int = Tariefeenheden.get_tariefeenheden(info.from_station, info.to_station)

        pricing_table = PricingTable.get_price_table()

        if info.travel_class == UIClass.FirstClass:
            if info.discount == UIDiscount.TwentyDiscount:
                price = pricing_table[1]
            elif info.discount == UIDiscount.FortyDiscount:
                price = pricing_table[2]
            else:
                price = pricing_table[0]

        elif info.travel_class == UIClass.SecondClass:
            if info.discount == UIDiscount.TwentyDiscount:
                price = pricing_table[4]
            elif info.discount == UIDiscount.FortyDiscount:
                price = pricing_table[5]
            else:
                price = pricing_table[3]

        #Double price if returnticket is applicable
        if info.way == UIWay.Return:
            price *= 2

        price = price * 0.02 * tariefeenheden
        return round(price, 2)

get\u price\u table
是一种超出范围的方法。因此,您只需导入适当的模块(如果该函数不在同一模块中)并直接调用它:
pricing\u table=get\u price\u table()

我的两分钱: 现在,
get\u price\u table
函数始终执行相同的操作,因此更好的方法是将其定义为映射或“常量”值:


发布追踪。我们不必猜测哪一行有错误。
PricingTable
未定义。它是一个模块吗?一节课?函数将
pricing_table
从浮动列表转换为字符串列表。。。当你使用它时,你想要哪个版本?我认为如果从一开始就为
pricing\u table
分配字符串,会更简单。将十进制浮点数转换为二进制浮点数可能会降低一些精度,因此最好尽可能避免。
PRICING_TABLE = [
  ...
]