Manogna has Published 66 Answers

What is unexpected indent in Python?

Manogna

Manogna

Updated on 26-Sep-2019 19:56:53

Python not only insists on indentation, it insists on consistent indentation. If we indent one line by 4 spaces, but then if we indent the next by 3 (or 5, 6, .), we get this error of unexpected indent in python.In the given code, line 3 has more spaces at ... Read More

How to catch multiple exceptions in one line (except block) in Python?

Manogna

Manogna

Updated on 26-Sep-2019 19:52:43

We catch multiple exceptions in one except block as followsAn except clause may name multiple exceptions as a parenthesized tuple, for exampletry: raise_certain_errors(): except (CertainError1, CertainError2, …) as e: handle_error()Separating the exception from the variable with a comma still works in Python 2.6 and 2.7, but is now deprecated and ... Read More

How to call a function of a module from a string with the function's name in Python?

Manogna

Manogna

Updated on 26-Sep-2019 19:44:27

The following code should call the required functionimport fibo result = getattr(fibo, 'baz') print resultSimilarly we can use getattr class instance bound methods, module-level methods, class methods and so on.

Why does Python code run faster in a function?

Manogna

Manogna

Updated on 30-Jul-2019 22:30:20

It is found that if python code is run normally and then if it is run in a python function, it runs faster in the latter case. I want to know why python code runs faster in a function.It is generally found that it is faster to store local variables ... Read More

How to check if a string contains only whitespace letters in Python?

Manogna

Manogna

Updated on 30-Jul-2019 22:30:20

We can check if a string contains only whitespace characters using 2 methods. First is using method isspace(). For example:

What are the best practices for exception handling in Python?

Manogna

Manogna

Updated on 30-Jul-2019 22:30:20

Some of the best practices for exception handling in python are given below:Exceptions are better than returning error status codes. We have to handle exceptions in Python as the whole language core and standard libraries throw exceptions. Elegantly handled exceptions are any day preferable to error codes and trace backs.It ... Read More

Advertisements