How to declare a variable in Python without assigning a value to it?

PythonServer Side ProgrammingProgramming

Python is dynamic, so you don't need to declare things; they exist automatically in the first scope where they're assigned. So, all you need is a regular old assignment statement that assigns None to the variable. 


my_var = None

If you use this, you'll never end up with an uninitialized variable. But this doesn't mean that you won't end up with incorrectly initialized variables. If you init something to None, make sure that's what you really want, and assign something more meaningful when you use it.

Published on 05-Jan-2018 12:31:42