Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a pointer is null in python?

I need to process a pointer to get its value via a callback. This is problematic since this pointer is null during the first call. So doing a pointer.contents does ValueError: NULL pointer access. To avoid this, how to check if the pointer is null ?

like image 443
Bogey Jammer Avatar asked Sep 03 '15 22:09

Bogey Jammer


People also ask

Can you check if a pointer is null?

Use Pointer Value as Condition to Check if Pointer Is NULL in C++ Null pointers are evaluated as false when they are used in logical expressions. Thus, we can put a given pointer in the if statement condition to check if it's null.

How do you check for null in Python?

Use the is operator to check if a variable is null in Python, e.g. if my_var is None: . The is operator returns True if the values on the left-hand and right-hand sides point to the same object and should be used when checking for singletons like None .

Does Python have null pointers?

There's no null in Python. Instead, there's None. As stated already, the most accurate way to test that something has been given None as a value is to use the is identity operator, which tests that two variables refer to the same object. In Python, to represent an absence of the value, you can use a None value (types.

Can a pointer to a pointer be null?

Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.


1 Answers

NULL Pointers have a boolean False value, so:

if pointer:
    x = pointer.contents
else:
    print "NULL pointer"

Edited for indentation

like image 76
Maxqueue Avatar answered Oct 20 '22 01:10

Maxqueue