What is the usage of none? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the usage of none?

5th Sep 2016, 5:29 AM
Navina Dhanasekar
Navina Dhanasekar - avatar
3 Answers
+ 2
for my idea ,it's useful when you declare some variable with empty value like max = None if a > b: max = a
5th Sep 2016, 1:52 PM
beauty1234
+ 1
Assigning a value of None to a variable is one way to reset it to its original, empty state. Often you will want to perform an action that may or may not work. Using None is one way you can check the state of the action later. Here's an example: database_connection = None # Try to connect try: database = MyDatabase(db_host, db_user, db_password, db_database) database_connection = database.connect() except DatabaseException: pass if database_connection is None: print('The database could not connect') else: print('The database could connect') >Another scenario would be where you may need to instantiate a class, depending on a condition. You could assign a variable to None, then optionally later assign it to an object instance. Then after that you may need to check if the class has been instantiated.
30th Dec 2016, 1:01 PM
Gourav
Gourav - avatar
0
Basically you can replace all your vars in a function that equal to 0 to None, so you have a better understanding of what they're supposed to be.
15th Nov 2016, 7:47 PM
Thomas Koppelaar
Thomas Koppelaar - avatar