-1
class CustomError(TypeError):
  def __init__(self,message,code):
    super().__init__(f'ErrorCode:{code}. {message}')
raise CustomError("This is a sample error message!",500)

Okay, so I have just started learning about custom errors in python and the instructor introduced me to the concept of error code. The problem is- I don't quite understand what this fstring is doing here? What does this mean?

  • 1
    It's like doing `"ErrorCode:" + code + ". " + message`, I believe – user May 12 '20 at 13:59
  • 1
    f-string is not about errors, it is a feature itself https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals. – Ekrem Dinçel May 12 '20 at 14:02
  • Does this answer your question? [Proper way to declare custom exceptions in modern Python?](https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python) – Anwarvic May 12 '20 at 14:04
  • It is the message yo pass to the `super` constructor. What is exactly your question? What are f-strings, or how to use exceptions? – Tomerikoo May 12 '20 at 14:17
  • What I'm not being able to get is- how is super constructor helping us print here? – IndischerPhysiker May 12 '20 at 14:18
  • Read about [exceptions](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) - *For convenience, the exception instance defines __str__() so the arguments can be printed directly without having to reference .args.*. You simply assign the message to the `.args` and they get printed. Try `super().__init__('x', 'y')` and see the difference – Tomerikoo May 12 '20 at 14:22
  • Of course you are free to implement your own `__str__` – Tomerikoo May 12 '20 at 14:23

2 Answers2

0

f-strings are a formatting syntax in python. You can (for example) use braces inside a string and put expressions to be evaluated. In this case there are variable names which get formatted into the string

Pani
  • 1,317
  • 1
  • 14
  • 20
0

Both f' and .format are format methods that are used for formatting string, f' is only applicable on strings on Python version 3.6 or more. When I use f'ErrorCode:{code}. {message}'), I am telling Python interpreter to take the string to be formatted in format, "Errorcode" : + code + "." + message. String format methods are used for styling input strings.

peeyush.cray
  • 15
  • 1
  • 6
  • Yes. That is true. But what I'm not being able to figure out is, how is super().__init__ helping in printing that. – IndischerPhysiker May 12 '20 at 14:15
  • @SarthakGirdhar It is not. `super().__init__` is the way to call the parent constructor. The `f-string` is simply its argument – Tomerikoo May 12 '20 at 14:18
  • Okay, so what exactly is helping to print the the result? – IndischerPhysiker May 12 '20 at 14:22
  • You class has a parent class Type Error, from which it is inheriting,the super constructor is being used to inherit methods from super class. Type Error is a builtin class from which it ins inheriting, you can use it for defining or create your own exceptions. – peeyush.cray May 12 '20 at 15:01