Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Classes without using def __init__(self)

Tags:

python

class

I am writing a series of text menus. With the class and sub class below it runs with no issues. But I am reviewing my coding and I am wondering....is it ok that I didn't use def __init__(self) in the classes? Should I have placed the data members in def __init__(Self): such as self.images = (), self.options =()? If I did that then I could not use the abc module for restrains, correct?

class BaseMenu(object):     __metaclass__ = abc.ABCMeta      @abc.abstractproperty     def options(self):         pass      @abc.abstractproperty     def menu_name(self):         pass      def display(self):         header = "FooBar YO"         term = getTerminalSize()         #sys.stdout.write("\x1b[2J\x1b[H")         print header.center(term, '*')         print self.menu_name.center(term, '+')         print "Please choose which option:"         for i in self.options:             print(                 str(self.options.index(i)+1) + ") "                 + i.__name__             )         value = int(raw_input("Please Choose: ")) - 1          self.options[value](self)  class Servers(BaseMenu):      menu_name = "Servers"     images = ()     foo = ()      def get_images(self):         if not self.images:             self.images = list_images.get_images()         for img in self.images:             print (                 str(self.images.index(img)+1) + ") "                 + "Name: %s\n    ID: %s" %                 (img.name, img.id)                 )      def get_foo(self):         if not self.foo:             self.foo = list_list.get_list()         for list in self.foo:             print "Name:", list.name             print "  ID:", list.id             print      def create_servers(self):          create_server.create(self)      options = (         get_images,         get_foo,         create_servers         ) 
like image 487
dman Avatar asked Jun 26 '13 21:06

dman


People also ask

Can a class exist without __ Init__ function?

You can still instantiate a class that doesn't specify the __init__ method. Leaving it out does not make your class abstract.

Can I create a class in python without init?

We can create a class without any constructor definition. In this case, the superclass constructor is called to initialize the instance of the class. The object class is the base of all the classes in Python.

What is __ init __( self in Python?

The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.

What is the need of def __ init __( self Param?

class Point: def __init__(self, x, y): self._x = x self._y = y. The __init__ method gets called after memory for the object is allocated: x = Point(1,2) It is important to use the self parameter inside an object's method if you want to persist the value with the object.


1 Answers

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.

like image 128
Martijn Pieters Avatar answered Sep 20 '22 20:09

Martijn Pieters