Is it possible only to declare a variable without assigning any value in Python?
Why not just do this:
var = None
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 as above.
This is nice, because you'll never end up with an uninitialized variable. But be careful -- 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 if you can.
I'd heartily recommend that you read Other languages have "variables" (I added it as a related link) – in two minutes you'll know that Python has "names", not "variables".
val = None
# ...
if val is None:
val = any_object