Manogna has Published 40 Articles

How do you test that a Python function throws an exception?

Manogna

Manogna

Updated on 12-Jun-2020 11:30:14

236 Views

We write a unittest that fails only if a function doesn't throw an expected exception.We also test if a Python function throws an exception.For example, see the sample code we paste into the Python shell to test Python's type-safety:Exampleimport unittest class MyTestCase(unittest.TestCase):    def test_1_cannot_add_int_and_str(self):       with self.assertRaises(TypeError): ... Read More

What are the allowed characters in Python function names?

Manogna

Manogna

Updated on 12-Jun-2020 11:27:30

1K+ Views

Python IdentifiersIdentifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.Rules for writing identifiersIdentifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore ... Read More

How to catch ValueError using Exception in Python?

Manogna

Manogna

Updated on 12-Jun-2020 07:56:10

264 Views

A ValueError is used when a function receives a value that has the right type but an invalid value.The given code can be rewritten as follows to handle the exception and find its type.Exampleimport sys try: n = int('magnolia') except Exception as e: print e print sys.exc_typeOutputinvalid literal for int() ... Read More

Which is more fundamental between Python functions and Python object-methods?

Manogna

Manogna

Updated on 13-Feb-2020 06:40:52

82 Views

A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method.  exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py A method is a special class of function, ... Read More

Can we explicitly define datatype in a Python Function?

Manogna

Manogna

Updated on 13-Feb-2020 06:27:43

443 Views

In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument.If we explicitly define datatype in a python function, ... Read More

How to implement a custom Python Exception with custom message?

Manogna

Manogna

Updated on 13-Feb-2020 05:11:20

186 Views

For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value ... Read More

Explain try, except and finally statements in Python.

Manogna

Manogna

Updated on 13-Feb-2020 05:03:24

413 Views

In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.If an exception occurs, the rest of the try block is skipped and the except clause is executed.Exampletry: 'apple' + 6 except Exception: ... Read More

How to catch KeyError Exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:59:45

676 Views

A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.Exampleimport sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())Output(, KeyError('c',), )

What is the correct way to pass an object with a custom exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:56:50

192 Views

In given code, a custom exception FooException has been created which is a subclass of the super class Exception. We will pass a string object to the custom exception as followsExample#foobar.py class FooException(Exception): def __init__(self, text, *args): super ( FooException, self ).__init__ ( text, *args ) self.text = text try: ... Read More

How to pass a variable to an exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:50:56

1K+ Views

Here we are passing a variable to given exception. We are defining a custom exception ExampleException which is a subclass of base class Exception and also defining the __init__ method. We use a try-except block to raise the exception and pass the variable to the exception as follows.Exampleclass ExampleException(Exception): def ... Read More

Advertisements