-1

I would like to get my subclass Sub class object from my api.py:

import inspect
from tm_base import Base
import tm_child

for k, v in vars(tm_child).iteritems():
if inspect.isclass(v):
    if (Base in inspect.getmro(v) and
                not inspect.isabstract(v)):
        print v

When running the api.py I get both:

<class 'tm_child.Sub'>
<class 'tm_base.Base'>

and I would like <class 'tm_child.Sub'> only, this is why I am using not inspect.isabstract(v). I do not see what is wrong with my code..

Here my other files.

tm_base.py

from abc import ABCMeta

class Base(object):
    __metaclass__ = ABCMeta

tm_child.py

from tm_base import Base

class Sub(Base):
    pass
diegus
  • 1,168
  • 2
  • 26
  • 57
  • Abstract classes have to have abstract methods; the simple presence of the ABCMeta metaclass doesn't make a class abstract. After all, Sub has ABCMeta as its metaclass too, inherited from Base. – user2357112 Sep 12 '17 at 17:08

1 Answers1

0

I've found it's not considered an abstract class until it has something:

class Base:
    __metaclass__ = ABCMeta

print(isabstract(Base)) #=> False

class Base:
    __metaclass__ = ABCMeta

    @abstractmethod
    def method(self):
        pass

print(isabstract(Base)) #=> True
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • More about this in [this](https://stackoverflow.com/questions/14410860/determine-if-a-python-class-is-an-abstract-base-class-or-concrete/14410942) SO question – Adelin Sep 12 '17 at 17:08
  • @Danil Speransky I added the abstract method decorator to the tm_base, now when I run my code nothing is printed out – diegus Sep 12 '17 at 18:21
  • @DanilSperansky could you edit your code with an example? Thank you for your help – diegus Sep 13 '17 at 12:54