The question is not about strongly typed languages etc. It is about starting to use variables anywhere in the program and change one not the other will result in a huge behavioral change. Jump to What if I am refactoring the code and change the variable name?
I am new to python (1 year experiance) but have been programming since 6 years with .Net and other languages.
In other languages to use a variable you define or declare a variable and initialize it before using like below :
C# : datatype variable_name = new datatype;
VB : dim variable_name as new datatype
java : datatype variable_name
c++ : datatype variable_name
In Python a variable is used like below :
def func():
is_name_appropriate = False
while not is_name_appropriate:
GLobal X
print("Hello World")
is_name_appropriate = "Haha what is happening here"
is_name_appropriate = True
is_surname_appropriate = False
X = "local variable under while"
print(X)
func()
print(X)
**Output :**
Hello World
local variable under while
local variable under while
As you can see is_name_appropriate
is directly used with out any definition of a Boolean datatype which allows me to assign it a string. I also used is_surname_appropriate
variable inside the while with whiles local scope by passing no explicit knowledge to the compiler. "The compiler is magical but humans are afraid of things they don't understand and so am I". I have started using a variable which has a global scope which in in the scope of the func's while and has no declaration. I can use x after calling this function anywhere. But I am still okay with this.
What if I am refactoring the code and change the variable name?
def func():
is_name_refactored_appropriate = False
while not is_name_refactored_appropriate:
GLobal X
print("Hello World")
is_name_appropriate = True # forgot to change this. I don't use IDE. terminal is great!!!
is_surname_appropriate = False
X = "local variable under while"
print(X)
func()
print(X)
**Output :**
Hello World
local variable under while
Hello World
local variable under while
Hello World
local variable under while
Hello World
local variable under while
.
.
.
and infinite
I did this mistake with IDE PyCharm Professional Edition. The bug generated was about to end my life.
Is there away to explicitly declare a variable so such variables generate a compile time error?
when Ii change a variable name and forget to change all and so the forget ones are errors. Not a question about strong types.
PS : Python class is also notorious with its parameters and if you have a solution for that too, your welcome to share. I can define them at top and all but i need compile time error if self.varible_name is used some where.
PS : I am okay with a plugin solution too, better is with python tho. Just want a a compile time error when such things occur.