Search This Blog

Showing posts with label Cool commands to know. Show all posts
Showing posts with label Cool commands to know. Show all posts

Sunday, March 16, 2014

None


None is :

  • a special constant in python
  • a null value
  • of datatype NoneType


None is not:

  • 0
  • is not an empty string
  • is not the same as False


You can:

  • compare None to anything other than None always returns False.
  • assign None to any variable
You cannot create other NoneType objects. All variables with a value of None are equal to each other.



Raising an Exception

Python uses try...except blocks to handle exceptions and raise statement to generate exceptions

We can raise exceptions by using the raise command of python.


Syntax:

raise [ Exception , [,args [,Traceback]]]


Exception : Type of exception like NameError or ValueError.
args :  value of exception argument
Traceback : It is very rarely uses and is the traceback object exception.

size=-1
if size < 1 :
raise ValueError('Number must not be negative')

Traceback (most recent call last):
  File "<pyshell#31>", line 2, in <module>
    raise ValueError('Number must not be negative')
ValueError: Number must not be negatrive

In reality, exceptions are implemented as classes and this raise statement is actually creating an instance of ValueError class and passing the string 'Number must not be negative' to its initialization method.