Python Classes without using def __init__(self)

Your code is perfectly fine. You don't 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 doesn't 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")

Tags:

Python

Class