0

I'm searching a way to create instances of an classe without using the __init__ by only getting an empty instance of the classe without attribute or with empty (None) attributes. The general idea is :

class InstanciableEmpty:
    def createEmptyInstance(self):
        class_to_instanciate = self.__class__
        #DO something
        return newEmptyInstance

class AnyOtherClass(InstanciableEmpty):
    def __init__(self):
         self.attr = 1

class AnyOtherClass2(InstanciableEmpty):
    def __init__(self, attr):
         self.attr = attr

The idea behind that is to create an generic classe wich can create copi of any sort of object without anything to write in the class who as herited from the generic classe.

Xiidref
  • 1,456
  • 8
  • 20
  • 2
    While there are certainly use cases for creating instances without calling `__init__`, I'm not convinced that this is one of them. May I ask what the point of creating an instance without any attributes is? – Aran-Fey Sep 30 '19 at 12:13
  • 2
    [XY problem](https://en.wikipedia.org/wiki/XY_problem) anyone ? Please explain your _real_ problem (the one you're trying to solve with this "feature"). – bruno desthuilliers Sep 30 '19 at 12:17
  • @brunodesthuilliers, agree, looks like an XY problem. – James Sep 30 '19 at 12:17
  • Sorry if I was not clear enough, the big idea behind this is just to create an generic classe that cas copy any other classes just by being inherited. I arleady write a code to copy all attribute from the generic classe but i still need to create a new instance to set the attributes manually. – Xiidref Sep 30 '19 at 12:25
  • The concept of classes are for attributes ,instances and relationships. Quite not a thought of 'generic' class ? – Kris Sep 30 '19 at 12:27
  • You can try a dynamic definition for your desired class: `InstanciableEmpty = type('InstanciableEmpty', (object,), {})` and then just create instances `obj = InstanciableEmpty()` – dejanualex Sep 30 '19 at 12:36

0 Answers0