-1

I didn't understand this line of the code below. This looks like a weird syntax to me.

super(CustomError, self).__init__(message, base_message, args)

class CustomError(Exception):
    """
    Abstract Base class for all exceptions raised in this ecosystem.

    """
    def __init__(self, message, base_message, *args):
        """
        :param message:
            Message to be displayed to user.

        :param base_message:
            Message to be passed to base class.

        :param args:
            Arguments to be passed to CustomError object.

        """
        super(CustomError, self).__init__(message, base_message, args)

Can someone please help me understand what this is doing internally? And what is the purpose of base_message and args.

Georgy
  • 12,464
  • 7
  • 65
  • 73
ThinkGeek
  • 4,749
  • 13
  • 44
  • 91
  • 2
    It looks like normal python to me. What looks weird to you? – Lie Ryan Dec 04 '18 at 11:20
  • 2
    Possible duplicate of [Understanding Python super() with \_\_init\_\_() methods](https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods) – Georgy Dec 04 '18 at 11:28
  • This question could be useful for people new to OOP in Python. A good explanation could be helpful instead of downgraded the question – MasterOfTheHouse Mar 10 '20 at 22:14
  • This also looked weird to David Beazley in its book. Lokesh is asking a valid an important question. I responded it here https://stackoverflow.com/questions/52509910/python3-calling-supers-init-from-a-custom-exception/60640425#60640425 – MasterOfTheHouse Mar 11 '20 at 16:26

1 Answers1

3

The CustomError class (Child class) inherits from the class Exception (Parent class). The line you quote calls the constructor of the Parent class. As it's called within the constructor of CustomError it means when an instance of CustomeError is created the constructor for the Parent class is also called.

The messages are different arguments to the exception.

It looks like custom error is bundling any additional arguments into a tupple.

See the example below:

        class CustomError(Exception):
            """
            Abstract Base class for all exceptions raised in this ecosystem.

            """
            def __init__(self, message, base_message, *args):
                """
                :param message:
                    Message to be displayed to user.

                :param base_message:
                    Message to be passed to base class.

                :param args:
                    Arguments to be passed to CustomError object.

                """
                super(CustomError, self).__init__(message, base_message, args)

        try:
            raise Exception('m1','m2',1,2,3)
        except Exception as e:
            print (e.args) #('m1', 'm2', 1, 2, 3)

        try:
            raise CustomError('m1','m2',1,2,3)
        except CustomError as c:
            print (c.args) #('m1', 'm2', ([1, 2, 3],))
Steve
  • 476
  • 3
  • 10
  • I am just confused why super has to take these arguments "super(CustomError, self)". – ThinkGeek Dec 04 '18 at 13:55
  • The syntax depends on which version of python is being used. The format above is used for maximum portability. In Python 3 syntactic sugar super() will do the same. See https://stackoverflow.com/questions/10482953/python-extending-with-using-super-python-3-vs-python-2 and the link to PEP3135 https://www.python.org/dev/peps/pep-3135/ – Steve Dec 04 '18 at 17:06