Manogna has Published 54 Articles

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

Manogna

Manogna

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

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

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

What are default arguments in python?

Manogna

Manogna

Updated on 12-Jun-2020 11:25:42

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default valueDefault arguments:ExamplePython has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument ... Read More

How to catch ValueError using Exception in Python?

Manogna

Manogna

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

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

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

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

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

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 handle an exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 05:00:21

The simplest way to handle exceptions in python is using the "try-except" block.Exampletry: fob = open("test.txt", "r") fob.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find the file or read data" else: print "Write operation is performed successfully on the file" fob.close()OutputError: can't find the ... Read More

How to catch KeyError Exception in Python?

Manogna

Manogna

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

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',), )

Advertisements