135 Antworten
+ 79
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.
+ 53
value = None;
value = 1;
+ 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.
+ 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...
+ 12
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.
+ 11
You can do
x = str
for example, but there is no obvious advantage in doing this as others have said.
+ 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!
+ 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
+ 7
Can you maybe show your case that makes you think you need it?
(Because I think you don't.)
+ 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. 😊
+ 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.
+ 7
After learning Python what Can i do with it ?
+ 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).
+ 5
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😃😃
+ 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.
+ 5
yes you can declare by following :
x = None
x = " " This is empty string
+ 5
Sandra Meyer
In that case you got a point, yes now I see the other way to look at the question : )
+ 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
+ 4
Code migration could be a use case too.
+ 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)