GG_Python - 6 months ago 48

Python Question

I need to define a class which *extends* python's standard math module, without instantiating it (no need for that, all methods in the class are static):

`import math`

class more_math(math):

@staticmethod

def add_func(x):

return math.sqrt(x)+1

The code above doesn't run properly (script exits), with the error:

`TypeError: Error when calling the metaclass bases`

module.__init__() takes at most 2 arguments (3 given)

When the class declaration above is set to

`class more_math:`

`more_math.add_func(x)`

`more_math.sqrt(x)`

`sqrt`

`math`

`more_math`

`math`

Ideas on how could this be setup properly?

Answer

Think long and hard about whether you actually need to provide the functions math implements. You almost certainly don't; you probably just need to provide *your* extras. That said, if you do need to provide a more_math module that implements all the standard math functions too, the easiest way to do this is to do a `from math import *`

. This will bring every function the `math`

module defines into your module. This is considered *very* bad practice, because it pollutes the namespace of your module and can make it difficult to tell what is actually being used. However, in this case, pollution of your module's namespace is exactly what you *want*.