views:

274

answers:

2

For some reason I am having trouble getting my head around __init__ and __new__. I have a bunch of code that runs fine from the terminal, but when I load it as a plugin for Google Quick Search Box, I get the error TypeError: default __new__ takes no parameters.

I have been reading about the error, and it's kind of making my brain spin. As it stands I have 3 classes, with no sub-classes, each class has it's own defs. I never use def __init__ or def __new__, but I have gotten the distinct feeling that these are the functions (or the lack thereof) that would be giving me the error.

I have no idea how to summarize the code down to a snippet that would be helpful here, since I'm a bit over my head, but the entire script can be found at github. Not expecting anyone to bugfix my code for me, I am just at my wit's end on this. A simple (plain english, not the quote from the python docs which I have read 20 times and still don't really understand) explination of why this error would pop up, or why I should be, or not be, using the __init__ and/or __new__ functions would be seriously appreciated.

Thanks for any help you can give in advance.

+4  A: 

Simplest way to reproduce your problem:

>>> class Bah(object): pass
... 
>>> x = Bah(23)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: default __new__ takes no parameters

So it looks like there's a class in your code (not when run from the terminal, but then you do very different imports then) that has no __init__ defined, and you're somewhere calling the class with some parameters. In the error traceback you should get the location where the call happens (if not, e.g. due to GUI subtleties, you can use a try/except around everything and make sure you dump the traceback to a /tmp file in the except clause, use module traceback from the standard library).

Alex Martelli
Thanks. Turns out that Google Quick Search Box was making a call to `init`, when it hadn't been in previous versions. Sorry my question wasn't formatted too well.
Gordon Fontenot
+1  A: 

There are a handful of basic class construction concepts that you seem to be missing.

The __init__ method is used when the class is first initialized, and is how you set up its internals. You also want to take advantage of Python's new-style classes (the default in Python 3).

I would suggest starting with Dive Into Python and branching out from there.

jathanism
Thanks for the help. I'm trying to understand this whole `__init__` vs `__new__` thing, but something is still missing for me. I'll give Dive Into Python a second look.
Gordon Fontenot