Variables in Python | Sololearn: Learn to code for FREE!

+25

Variables in Python

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

7/25/2020 1:26:40 PM

Suhaila Abdulkareem

67 Answers

New Answer

+39

Python is dynamic typed language, there's no type definition needed when declaring variables. Type of data for a certain variable is concluded from the type of value (object) assigned to it. How would Python decide a variable's type when there's no value (object) being assigned to it? I have no idea.

+27

value = None; value = 1;

+16

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...

+15

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.

+11

You can do x = str for example, but there is no obvious advantage in doing this as others have said.

+9

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.

+9

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 it, even if the thing to be assigned was of a None type.

+8

Can you maybe show your case that makes you think you need it? (Because I think you don't.)

+6

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

+6

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.

+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

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. 😊

+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

+5

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!

+3

Code migration could be a use case too.

+3

yes you can declare by following : x = None x = " " This is empty string

+2

variable = None