Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to pass None to a parameter?

Tags:

python

People also ask

Can I pass None as an argument in Python?

None is often used as a default argument value in Python because it allows us to call the function without providing a value for an argument that isn't required on each function invocation.

How do you pass a null as a parameter?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.

What does parameter None mean in Python?

Setting a parameter's default to None is commonly used when a new mutable object is required. If you set a default to a mutable object (like a list) then that same single object is reused throughout the life of the program. Using None is a pythonic way of indicating that a new object is required.

Can you pass a list as a parameter?

You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.


There's nothing wrong with using None to mean "I am not supplying this argument".

You can check for None in your code:

if c is None:
    # do something
if d is not None:
    # do something else

One recommendation I would make is to have None be the default argument for any optional arguments:

def myfunct(a, b, e, f, c=None, d=None):
    # do something

myfunct(A, B, E, F)