簡體   English   中英

來自 def 的調用列表

[英]Call list from def

我有這個定義:

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_price_table是在范圍外定義的方法。 所以你只需要導入正確的模塊(如果該函數不在同一個模塊中)並直接調用它: pricing_table = get_price_table()

我的兩分錢:就像現在一樣, get_price_table函數總是做同樣的get_price_table ,所以更好的方法是將它定義為地圖或“常量”值:

PRICING_TABLE = [
  ...
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM