class inheritance python2.7 vs python3.3

Dave Angel davea at davea.name
Mon Jan 6 12:46:39 EST 2014


On Mon, 6 Jan 2014 09:14:08 -0800 (PST), jwe.van.dijk at gmail.com wrote:
> I have problems with these two classes:


> class LPU1() :

You forgot to derive from object. That's implied on 3.x, but you say 
you're also running on 2.7  Without naming your base class you're 
asking for an old style class which has been obsolete maybe 10 years. 
I sure don't recall how it differs.

>     def __init__(self, formula):
>         """
>         formula is a string that is parsed into a SymPy function
>         and several derived functions
>         """
>         self.formula = formula
>         ... ...


> class LPU3(LPU1):
>         def __new__(self):
>         """
>         the same functions as LPU1 but some added functions
>         and some functions redefined

You don't show where you call super, so we can't tell what you had in 
mind. And did you actually mean __new__ here or should you have 
defined __init__ as you did in the base class?

> if __name__ == '__main__:
>     y = y = 'x_0 * x_1 + x_2'
>     stats1 = LPU1(y)
>     stats3 = LPU3(y)

And where did you expect that y to go?


> Worked perfectly on Python 2.7.5+ but on Python 3.3.2+ I get on 
instantiatiating stat3:

I don't see anything called stat3. Presumably you mean stats3, but 
you're not instantiating it you're instantiating LPU3.

> TypeError: __new__() takes 1 positional argument but 2 were given

You forgot to include the rest of the stack trace.


I think the real problem is you forgot to include the second 
parameter on the misnamed __init__ method. It should have parameters 
self and arg, and pass arg up through super.

-- 
DaveA




More information about the Python-list mailing list