GG_Python GG_Python - 2 months ago 21
Python Question

Inheriting math methods into a class with only static methods

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):

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:
is called without error. However,
is a method of
] can't be called, as
doesn't have
as its base class.

Ideas on how could this be setup properly?

gbe gbe

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.