[Tutor] Error in class definition of __init__

Leo Silver leo.silver at soholinux.com.au
Wed Feb 14 20:27:16 EST 2018


Hello.

I'm trying to create a class to represent products which includes a list of
volume based pricing and sets the first of these as the unit price:

    def __init__(self, RatePlanID):
        self.id = RatePlanID
        self.name = RatePlans.toggleids[self.id]
        self.pricing = RatePlans.pricebreaks[self.name]
        self.unitprice = RatePlans.pricebreaks[self.name][0]

This code gives an IndexError:
...
    self.unitprice = RatePlans.pricebreaks[self.name][0]
IndexError: list index out of range

However, the same code with the last line changed to:
        self.unitprice = RatePlans.pricebreaks[self.name][:1]

seems to work OK, although, I can't process it further and extract the
second element of the pair.

The list I'm trying to process is:
[(1, 21.0), (100, 19.6), (250, 18.4), (500, 17.6), (1000, 16.7), (2500,
14.7), (10000, 13.7)]

and a cut and paste into the IDLE GUI let's me process it exactly as I
expect (including picking the second element of the tuple, the price rather
than the volume level):
>>> [(1, 21.0), (100, 19.6), (250, 18.4), (500, 17.6), (1000, 16.7), (2500,
14.7), (10000, 13.7)][0][1]
21.0

What am I missing about the class definition that won't let me put this
class in the init call?

Thanks, Leo.


More information about the Tutor mailing list