How can I implement a list of subclass from a class in python's owlready2?

763 Views Asked by At

I want to get all the subclasses for each class in a ontology built on protege and imported into python using owlready2. How can I create a list of all the subclasses for a set class of this ontology ?

2

There are 2 best solutions below

0
On

Just use list(class_name.subclasses())

1
On
You can dynamically create classes or sub classes.

import types
class_name = "MyClass"
parent_class = [Thing]
with onto:
    NewClass = types.new_class(class_name, tuple(parent_class))


Please note the new_class() function expects a tuple with the parent
classes and reference of parent class will be passed not string. If your parent class present inside the ontology you can access (onto["parent_class"])