+ 2
What is the usage of none?
3 odpowiedzi
+ 2
for my idea ,it's useful when you declare some variable with empty value
like
max = None
if a > b:
max = a
+ 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.
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.