Variables in Python
Can a variable be declared in Python without initializing a value for it? If possible, please explain how.
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...
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.
You can do x = str for example, but there is no obvious advantage in doing this as others have said.
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!
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
Can you maybe show your case that makes you think you need it? (Because I think you don't.)
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).
After learning Python what Can i do with it ?
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
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)
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.
Why is my answer only coming out right for one test case even when I use the recommended one
Python palindrome challenge.
Pls what's wrong?
help my code does not iterate