107 AnswersNew Answer
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.
value = None; value = 1;
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.
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...
You can do x = str for example, but there is no obvious advantage in doing this as others have said.
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.
Not possible as far as i know. But consider this def fun(): global x x = 5 return fun() print(x) # x available from inside the function in the outer scope due to global keyword. But this is something else, i don't think you are looking for this.
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!
Can you maybe show your case that makes you think you need it? (Because I think you don't.)
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
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).
Suhaila Abdulkareem I don't think there is, as Ipang 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😃😃
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.
yes you can declare by following : x = None x = " " This is empty string
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
After learning Python what Can i do with it ?
Sandra Meyer In that case you got a point, yes now I see the other way to look at the question : )
Code migration could be a use case too.