Saturday, 15 May 2010

python - TypeError when calling superclass's __init__ method -



python - TypeError when calling superclass's __init__ method -

i somehow getting weird error when i'm trying create instance of class.

... file <file1>, line 242, in some_method created_object = subclass(obj1, obj2) file <file2>, line 11, in __init__ superclass.__init__(self, obj1) typeerror: expected string or buffer

here's 2 __init__ methods like:

class subclass(superclass): def __init__(self, obj1=none, obj2=none): superclass.__init__(self, obj1) ...

and

class superclass(object): def __init__(self, obj1=none, obj2=none): self.obj1 = obj1

the same code in "file1" creates instance of subclass creates instances of other subclasses of same superclass. receive similar objects arguments, 1 of subclasses causing error. additionally, @ to the lowest degree 1 of working subclasses has exact same definition , first line __init__ method.

this error showed after made changes in "file1", did not touch way in instantiating subclass objects or objects pass in them.

in case, don't understand error. expecting string or buffer, , why?

can shed lite on error?

edit:

self.subclass = getattr(importlib.import_module(subclass_module), 'subclass') del sys.modules[subclass_module] # premature optimization strikes again!

as explained in reply posted, figured out above causing error occur. however, little digging has left me more confused error itself. changed __init__ method this:

def __init__(self, obj1=none, obj2=none): print none.__init__ none.__init__('hello', 'world')

and called this:

print none.__init__ none.__init__('hello', 'world') obj = self.subclass(obj1, obj2)

and i'm still getting typeerror in __init__ method, but not when phone call right before that. here 2 lines disassembled using dis module

# none.__init__('hello', 'world') in subclass.__init__ 10 9 load_const 0 (none) 12 load_attr 1 (__init__) 15 load_const 1 ('hello') 18 load_const 2 ('world') 21 call_function 2 24 pop_top # none.__init__('hello', 'world') right before seek create subclass object 246 147 load_const 0 (none) 150 load_attr 7 (__init__) 153 load_const 5 ('hello') 156 load_const 6 ('world') 159 call_function 2 162 pop_top

the output of 2 prints set in this:

<method-wrapper '__init__' of nonetype object @ 0x102977538> <method-wrapper '__init__' of nonetype object @ 0x102977538>

so it's same __init__ method each time called constants, different results. going on here?

changing super(subclass, self).__init__(obj) didn't prepare problem did alter error this:

typeerror: must type, not none

based on learned previously alter caused issue (probably premature) memory optimization trying - deleting module sys.modules. along came garbage collection, , both subclass , import of superclass became none. leaving module in sys.modules eliminates problem.

knowing cause of error doesn't help me understand error though. still don't understand why getting typeerror: expected string or buffer.

python python-2.7

No comments:

Post a Comment