+ 1

Class and object at the same time in Python?

Hi! Supposing the following example: we have the object Spike which belongs to the class Doberman which inherits class Pet which inherits class Dog which inherits class Four_Legs_Animal which inherits NotFoodAnimal which inherits class Animal which inherits class Life. Life--->Animal--->NotFoodAnimal--->Four_Legs_Animal--->Dog--->Pet--->Doberman--->Spike. Supposing your python program doesn't have defined each class and it is reading some text from a file. From that text Animal can be a final object like Spike is and if it doesn't read further it thinks that Animal is the final object so the class Animal can be an object at anytime. But it reads more text and it finds out that there are more kinds of animals with two and four legs and some of them can be food and others don't. So it creates by itself these classes accordingly. But it reads further and its conclusion that the final class could be Dog. So creates the Dog class as an object. Further it finds out that Spike is the final object. How to do this?

15th Sep 2019, 10:16 AM
Constantin Ghineț
Constantin Ghineț - avatar
3 Réponses
+ 2
I will study it further. Thank you so much for your help!
15th Sep 2019, 2:39 PM
Constantin Ghineț
Constantin Ghineț - avatar
+ 1
Thank you for your nicely explained answer! It seems that you didn't understand the main thing (or maybe I don't fully understand your answer, will see): All these classes are not already defined into the python script. It reads a simple text story about an animal and further it finds out that the animal is a Dog and much more further it finds out that the dog have a name (Spike). It creates all these classes on the fly, at the runtime thinking every time the last one is an object. So, it reaches the conclusion that it should create an object named animal of the class (possible hard coded and already defined) life then it decide that animal is a class too and make it as class and further it creates object dog of the class fourlegsanimal but much further transform it into another class in this hierarchy and make the final object Dog. Finally it stores someway all these classes and objects so it could be able to use them every time it runs. If it could rewrites itself it would be PERFECTLY!
15th Sep 2019, 1:11 PM
Constantin Ghineț
Constantin Ghineț - avatar
+ 1
Could you help me please? Thank you so much for all of these!
15th Sep 2019, 1:12 PM
Constantin Ghineț
Constantin Ghineț - avatar