class – Python Classes without using def __init__(self)

class – Python Classes without using def __init__(self)

Your code is perfectly fine. You dont have to have an __init__ method.

You can still use __init__, even with an ABC. All that the ABC meta tests for is if the names have been defined. Setting images in an __init__ does requires that you define a class attribute, but you can set that to None at first:

class Servers(BaseMenu):

    menu_name = Servers
    images = None
    foo = None

    def __init__(self):
        self.images = list_images.get_images()
        self.foo = list_list.get_list()

Now you can set constraints on the ABC requiring that a images abstract property be available; the images = None class attribute will satisfy that constraint.

Your code is fine. The example below shows a minimal example.
You can still instantiate a class that doesnt specify the __init__ method. Leaving it out does not make your class abstract.

class A:
    def a(self, a):
        print(a)
ob = A()
ob.a(Hello World)

class – Python Classes without using def __init__(self)

Leave a Reply

Your email address will not be published. Required fields are marked *