0

I have a list of strings, which all are modules that are imported (I parsed the __init__.py file earlier for some other validation). For example mods = ['mod_one', 'mod_two', 'mod_three']. I now need to verify that all of these are an instance of a pre-defined superclass (skeleton.py) - like

for x in mods:
    if not isinstance(x.Operation,skeleton.OperationSkeleton): 
        print("error with: "+x)`

My problem is that x is in this case a string - obviously, but is the name of the imported module I want to check. The string doesn't have a Operation..

And odd enough.. for testing purpose did I try to

print(isinstance(mod_one.Operation,skeleton.OperationSkeleton))

and it prints False. But in mod_one.py the OperationSkeleton is imported from skeleton and class Clustering(ClusteringSkeleton): is created.

3
  • 1
    class Clustering is not the same thing as class.Operation. Commented Oct 21, 2014 at 12:50
  • whoops. thanks for editing. But in mod_one.py the OperationSkeleton is imported from skeleton and class Operation(OperationSkeleton): is created. Commented Oct 21, 2014 at 13:01
  • 1
    Right, in which case isinstance() would just work. But do make sure you didn't accidentally added a package subdirectory to your path and imported the module under two different names (package.module and module), for example. You can verify you have the same class by looking at the id(skeleton.OperationSkeleton) and also look at skeleton.__name__ and skeleton.OperationSkeleton.__module__ in both locations. Commented Oct 21, 2014 at 13:03

1 Answer 1

1

If your modules are already imported, you can access them by their name in the sys.modules mapping:

import sys

for x in mods:
    mod = sys.modules[x]
    if not issubclass(mod.Operation, skeleton.OperationSkeleton): 

You need to use issubclass() here as Operation is not an instance.

Sign up to request clarification or add additional context in comments.

6 Comments

works perfectly fine! But I still get a False on print(isinstance(sys.modules["my_modules.mod_one"].Operation, skeleton.OperationSkeleton))
@DaedalusMythos: see my comment on your question; clearly you are not using the same class object to base the Operation class on as what you are using for isinstance().
@DaedalusMythos: Another way you could introspect this is sys.modules["my_modules.mod_one"].Operation.__bases__; that is a tuple of base classes for Operation. Again look at things like the output of id() to see what objects are actually being used here.
I'm a little overwhelmed but grateful for the help! I learned that both __name__s use the my_modules. in front. But even if i print(isinstance(sys.modules["my_modules.mod_one"].Operation, sys.modules["my_modules.skeleton"].OperationSkeleton) it's False. What am I missing?
@DaedalusMythos: Sorry, I am being slow. You don't have an instance, you have a class, so use issubclass().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.