Assigning instances to a dynamically created subclass

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Assigning instances to a dynamically created subclass

cdemir
Hello Jean-Baptiste

I was wondering, how one could assign **some** instances of a class to its subclass. Consider the following example

for i in onto.classes():

    NewClass = types.new_class("dummysub{0}".format(i), (i,))

    print(i) # => family.Brother
    print(NewClass) # => family.dummysubfamily.Brother
    print(NewClass.is_a) # => [family.Brother]
    print(i.instances()) # =>  [family.F10M173, ...,family.F10M183] == i.direct_instances()

    print(NewClass.instances()) # => []
    print(NewClass.direct_instances()) # => []

(1) What is the reason of such naming convention  family.dummysubfamily.Brother?
(2) How could I assign/add some instance from Brother class to the new class?

Cheers,
Caglar Demir
Reply | Threaded
Open this post in threaded view
|

Re: Assigning instances to a dynamically created subclass

Jiba
Administrator
Hello,

(1) in the format argument, you passed the class i, thus the class is rendered as a string. By default, Owlready renders its as "family.Brother", hence the result you obtain.

You may pass i.name if you only want the class name.

(2) to add a new class to an individual, you can simply add to the individual .is_a list, e.g. :

for j in i.direct_instances(): j.is_a.append(NewClass)

You may also remove classes from the list.

Jiba