+ 42

Variables in Python

Can a variable be declared in Python without initializing a value for it? If possible, please explain how.

25th Jul 2020, 1:26 PM
Suhaila Abdulkareem
Suhaila Abdulkareem - avatar
125 Answers
+ 71
Python is a dynamic typed language, there's no type definition needed when declaring variables. Type of data for a certain variable is concluded (deducted?) from the type of value (object) assigned to it. How would Python decide which type to be used for a variable when there's no value (object) being assigned to it? I have no idea.
25th Jul 2020, 1:36 PM
Ipang
+ 52
value = None; value = 1;
25th Jul 2020, 1:33 PM
Sandra Meyer
Sandra Meyer - avatar
+ 25
Sandra Meyer I think you know more than well enough that, whenever a type being set as RHS operand of assignment operator, it will initialize the LHS operand (e.g. a variable). Even `None` is a type in Python, so we can't say assigning `None` to something means declaration without initialization, the assignment operation obviously initializes the LHS. The original post asked whether declaration without initialization (have something assigned to it) was possible, as I understand, in Python it is not possible, because of the dynamic typing nature of the language. This is not about whether something in Python was considered a proper replacement for something else done in different language having different paradigm about types.
26th Jul 2020, 2:14 PM
Ipang
+ 21
Ipang I did just interprete the question as less theoretical, but more as "without an explicit value" (so any sort of null-equiv OR really un-initialized). For the theoretical discussion you're completely right of course...
26th Jul 2020, 2:20 PM
Sandra Meyer
Sandra Meyer - avatar
+ 13
For everyone saying <variable> = None is declaration without initialization, you are wrong. The assignment operator = is there, meaning something is being assigned to the variable, which defines the variable type and initializes the underlying data, even if the thing to be assigned was of a None type.
26th Jul 2020, 10:56 AM
Ipang
+ 12
You can do x = str for example, but there is no obvious advantage in doing this as others have said.
25th Jul 2020, 2:01 PM
Russ
Russ - avatar
+ 9
In c/c++ We would do something like this int a; Here we're declaring a and the default value of a is 0. But in python we can't do something like this. But we can do this: a = int() Here the default value is 0. Hope u got your ans. Happy coding!
27th Jul 2020, 5:35 AM
MSN
MSN - avatar
+ 8
Sousou The first line you showed employed type hint. But without initialization (as per the original post Decription), type hint doesn't mean anything, and the variable will still be unrecognized. my_name: str # type hint - uninitialized my_money: float # type hint - uninitialized print( my_name , ' has', my_money ) # output: Error 'my_name' is not defined
25th Jul 2020, 4:10 PM
Ipang
+ 7
Can you maybe show your case that makes you think you need it? (Because I think you don't.)
25th Jul 2020, 1:41 PM
HonFu
HonFu - avatar
+ 7
Thank you all.. As I learned coding in Java, C++ and JavaScript first, I hv been used used to the method of declaring a variable first and then initializing it later on in the program. So, I was a bit confused when I couldn't do that in Python. ๐Ÿ˜Š
26th Jul 2020, 7:11 AM
Suhaila Abdulkareem
Suhaila Abdulkareem - avatar
+ 7
C etc. are obviously different, but in Javascript, the declaration (with or without initialization) decides about where the value is available. Like if you use no marking, it will be 'global', if you use var, it will be local and hoisted, and with let it will be limited to the scope and not hoisted (I hope I got that right). In Python, a variable is by default put where you assign it. So if you do it in the global area, it will be global, if you do it in a function or method, it will be local - unless you say otherwise. So there's really never a need to declare a variable before you use or at least initialize it.
26th Jul 2020, 8:09 AM
HonFu
HonFu - avatar
+ 7
After learning Python what Can i do with it ?
6th Jun 2021, 9:19 AM
AY Oub
AY Oub - avatar
+ 6
You're basically right Ipang , but it's a proper replacement for all that lines with declaration and initialization of arrays, maps, etc and all the = null assignments. In fact it is equal to an assignment with null / Nil (except the variable still can change its type dynamically).
26th Jul 2020, 12:59 PM
Sandra Meyer
Sandra Meyer - avatar
+ 5
Suhaila Abdulkareem I don't think there is, as [user id="5117788"]Ipang[/user] rightly stated. A value has to be assigned to a variable for Python to know that...ah, this is a string, or this is a bool type๐Ÿ˜ƒ๐Ÿ˜ƒ
25th Jul 2020, 1:44 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 5
Yeah, that would be initializing all over again. x = 5 is written to the globals, so that's the same as if you just wrote it into the main code.
25th Jul 2020, 1:47 PM
HonFu
HonFu - avatar
+ 5
yes you can declare by following : x = None x = " " This is empty string
26th Jul 2020, 10:00 AM
Mr. 12
Mr. 12 - avatar
+ 5
Python is Dynamically typed language. It doesn't allow variable declaration without initialization which makes it very slow. There is another version of python called as Cython in which you can declare variables without intializing which makes your code lot faster
27th Jul 2020, 3:45 AM
Aravind Shetty
Aravind Shetty - avatar
+ 4
Yes, declaring a variable without initializing a value to that variable is possible. Python has a datatype "None" which makes this possible. Try this out. variable=None print(variable)
26th Jul 2020, 7:00 AM
Avishkha Reddy
Avishkha Reddy - avatar
+ 4
Sandra Meyer In that case you got a point, yes now I see the other way to look at the question : )
26th Jul 2020, 2:30 PM
Ipang
+ 3
Code migration could be a use case too.
25th Jul 2020, 1:46 PM
Sandra Meyer
Sandra Meyer - avatar