Python error while initializing a class derived from and abstract

I have this simple code and I am getting a strange error:

from abc import ABCMeta, abstractmethod

class CVIterator(ABCMeta):

    def __init__(self):

        self.n = None # the value of n is obtained in the fit method
        return


class KFold_new_version(CVIterator): # new version of KFold

    def __init__(self, k):
        assert k > 0, ValueError('cannot have k below 1')
        self.k = k
        return 


cv = KFold_new_version(10)

In [4]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-ec56652b1fdc> in <module>()
----> 1 __pyfile = open('''/tmp/py13196IBS''');exec(compile(__pyfile.read(), '''/home/donbeo/Desktop/prova.py''', 'exec'));__pyfile.close()

/home/donbeo/Desktop/prova.py in <module>()
     19 
     20 
---> 21 cv = KFold_new_version(10)

TypeError: __new__() missing 2 required positional arguments: 'bases' and 'namespace'

      

What am I doing wrong? A theoretical explanation would be appreciated.

+3


source to share


1 answer


You have used the meta tag incorrectly ABCMeta

. This is a meta class, not a base class. Use it as such.

For Python 2, this means assigning an attribute __metaclass__

to it for the class:

class CVIterator(object):
    __metaclass__ = ABCMeta

    def __init__(self):
        self.n = None # the value of n is obtained in the fit method

      

In Python 3, you must use the syntax metaclass=...

when defining a class:



class CVIterator(metaclass=ABCMeta):
    def __init__(self):
        self.n = None # the value of n is obtained in the fit method

      

As with Python 3.4, you can use a abc.ABC

helper class
as your base class:

from abc import ABC

class CVIterator(ABC):
    def __init__(self):
        self.n = None # the value of n is obtained in the fit method

      

+9


source







All Articles