0

So I have a class Panini, which I have to initialize, but it keeps getting stuck in the line assert isinstance(stickers, (int, tuple, set, list)), 'invalid stickers' and when I remove the line it gets stuck in the next isinstance, can somebody tell me what I'm doing wrong here?

class Panini:
    def __init__(self, stickers):
            self.stickers = stickers
            #test if stickers is integer or a list/tuple/set of integers
            assert isinstance(stickers, (int, tuple, set, list)), 'invalid stickers'
            if isinstance(stickers, (set, tuple, list)):
                try: int_list = [int(x) for x in stickers]
                except ValueError:
                    raise 'invalid sticker'

            #if list or tuple contains duplicate integers(stickers) then these must be included in the collection only once
            if isinstance(stickers, (list, tuple)):
                my_set = set(stickers)
                if isinstance(stickers, tuple): stickers = tuple(my_set)
                else: stickers = list(my_set)
  • What are you initializing it with? Side-note: If the goal is just to handle `int`s separately from "iterable things", explicitly requiring the iterable to be `list`, `tuple` or `set` is pointlessly restrictive. You could just try to use `operator.index` on `stickers` (if it succeeds, it was `int`-like), and if that raises, try to iterate `stickers` (if that fails, the exception will propagate so people know it needs to be iterable). [EAFP](https://en.wikipedia.org/wiki/EAFP) is a good pattern, vs. explicit whitelisting. – ShadowRanger Dec 04 '15 at 13:56
  • Also, if this is Python 3.x as the tags indicate, `raise 'invalid sticker'` is invalid code. You can only `raise` `BaseException` and subclasses thereof; you'd want something like `raise ValueError('invalid sticker')` (or just let the original `ValueError` propagate instead of catching it at all. – ShadowRanger Dec 04 '15 at 14:02
  • Also, after the first line, you're reassigning `stickers` a lot, but not `self.stickers`, so the created object would have the unprocessed `stickers` as an attribute, and the rest of this whole function is just doing tests and conversions that, on success, throw away their results. – ShadowRanger Dec 04 '15 at 14:14
  • thanks, changed the syntax of raise, and also changed the place where I assign self.stickers, ( I'll put it in an update in a couple of minutes, and also what exactly happens now when I debug) –  Dec 07 '15 at 13:16
  • I think I'll just rewrite the program as a whole, but thanks for the help, I'm a beginner at this and can't really locate the error right now anymore –  Dec 07 '15 at 13:18

0 Answers0