Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python TypeError: __new__() missing 1 required positional argument: 'namespace'

I try to initialize class using

tsf = TimeSeriesFeature(cut_avg, interval)

but I got TypeError: __new__() missing 1 required positional argument: 'namespace'.

The init code of TimeSeriesFeature is below:

class TimeSeriesFeature(BasicFeature):
    def __init__(self, cut_avg, interval):
        super().__init__()
        self.cut_avg = cut_avg
        self.interval = interval
        self.get_avg()

and BasicFeature init code:

class BasicFeature(ABCMeta):
    def __init__(self):
        self.times = {}
        self.avg = {}

and I'm using python3.5. Is there any mistake?

like image 304
modkzs Avatar asked Aug 05 '26 22:08

modkzs


1 Answers

The error you're getting is from the fact that instantiation of metaclass does not use it's __init__ method, but rather it's __new__ method which (excluding self) expects 3 arguments, last being said namespace.

As it stands now your class inherits from ABCMeta (which is a metaclass), therefore it is also a metaclass:

class BasicFeature(ABCMeta):
    ...

but I think you'd rather set ABCMeta as a metaclass of your class:

class BasicFeature(metaclass=ABCMeta):
    ...

That way your class becomes an ordinary class, with ABCMeta as it's metaclass.

If you'd like BasicFeature to be a abstract (in other words impossible to initialize) class, add an abstract method to it, like this:

class BasicFeature(metaclass=ABCMeta):

    @abstractmethod
    def get_avg():
        pass

and overload it in subclass without the @abstractmethod decorator:

class TimeSeriesFeature(BasicFeature):

    def get_avg():
        # ... some code
        # ... some code

Then, users who attempt to instantiate BasicFeature will get an error about get_avg() being abstract, but users who instantiate TimeSeriesFeature will not. More details available in the documentation

like image 169
mbdevpl Avatar answered Aug 08 '26 11:08

mbdevpl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!