1

I try to use abstract in python, I created base class and concrete class. After I write register method, it shows Undefined variable from import: register. I suppose register is only a method, which library I shall import?

concreteTest.py:

import baseTest

class concreteTest(baseTest):

    def __abcMethodTest__(self):
        print("I am in concrete class")

baseTest.register(concreteTest)

if __name__ == '__main__':
    print 'Subclass:', issubclass(concreteTest, baseTest)
    print 'Instance:

baseTest.py:

from abc import ABCMeta
from abc import abstractmethod

class baseTest:
    __metaclass__ = ABCMeta

    @abstractmethod
    def __abcMethodTest__(self):
        while False:
            yield None
llrs
  • 3,308
  • 35
  • 68
user84592
  • 4,750
  • 11
  • 55
  • 91

1 Answers1

2

You have a class called baseTest within a module called baseTest, so after importing the module baseTest, you would need to use baseTest.baseTest to access the class:

baseTest.baseTest.register(concreteTest)

Note that you don't even need to register concreteTest as a virtual subclass, since it's already an actual subclass. The register() method is meant to register classes that don't actually derive from an abstract class you defined, but do fulfill the interface.

That said, it is unlikely that you routinely want to use abstract classes in Python. They are useful in some special circumstances, but aren't used as the standard way of declaring interfaces. See also the blog post Python is not Java.

Tim
  • 41,901
  • 18
  • 127
  • 145
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • The suggestions is really good. I shall not think Python as Java. But even I use baseTest.baseTest.register..., it has same error. – user84592 Jun 17 '14 at 10:46
  • @user I edited the question, but the title is still not clear. Also I did highlight the error you said, but I am not sure if this is or it was part of your explanation... – llrs Jun 17 '14 at 10:57
  • 1
    @user84592 Are you using PyDev? If you are, ignore this message and try to run it anyway, it should work fine! – evuez Jun 17 '14 at 11:05
  • @evuez Traceback (most recent call last): File "D:\NSN\trainingAndProjects\python\PythonWorkspace\com.nsn.mme.juhani.exer\concreteTest.py", line 2, in class concreteTest(baseTest): TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) – user84592 Jun 17 '14 at 11:11
  • Do I missing initialization? – user84592 Jun 17 '14 at 11:12
  • @Sven Marnach Anyway, Python is not Java, I shall take serious that writing Python in python way. You point very good articles. – user84592 Jun 17 '14 at 11:15
  • Now it is working after taking all suggestions by experts. from baseTest import baseTest class concreteTest(baseTest): ''' ''' def __abcMethodTest__(self): print("I am in concrete class") baseTest.register(concreteTest) if __name__ == '__main__': print 'Subclass:', issubclass(concreteTest, baseTest) print 'Instance:', isinstance(concreteTest(), baseTest) – user84592 Jun 17 '14 at 11:19