0

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.

Aagam Sheth
  • 685
  • 7
  • 15
  • 1
    I am surprised that you haven't read about Python being dynamically typed so far, given that you have been working with it for ~1 year now. – Selcuk Nov 19 '20 at 05:21
  • @Selcuk I have heard about that and i totally aware of that fact. Which is totally wrong concept according to me. But I wanted to ask something different. While I am refactoring I forgot to change one variable and now it is a new variable breaking the whole system. – Aagam Sheth Nov 19 '20 at 05:35
  • 2
    Well, there are dynamically typed (untyped) languages and statically typed ones. The "right" and "wrong" is debatable, but if you feel you are more productive with a statically typed language you should work with one. This is coming from someone experienced with both camps. – Selcuk Nov 19 '20 at 05:39
  • Also note that Python **is** a strongly typed language. – Selcuk Nov 19 '20 at 05:43
  • @Selcuk type hints in python is really great but its not the solution to my problem. – Aagam Sheth Nov 19 '20 at 06:18
  • 1
    The closest thing you can have is a good IDE such as PyCharm that will warn you about such oversights. – Selcuk Nov 19 '20 at 06:40
  • And of course testing, testing and testing. – Matthias Nov 19 '20 at 06:59
  • @Matthias Testing is not a solution and we use BDD. So testing is not that efficient as tests don't mean that much. – Aagam Sheth Nov 19 '20 at 07:12
  • @Selcuk PyCharm is really good in refactoring in such occasions but it asks for manual review in some chases and this is not feasible in a large 30,000 line code where a name is used more than 50 times and is used 100 times with other references. Thank you for being so humble and trying to suggest solutions. you Made my day. Thank you – Aagam Sheth Nov 19 '20 at 07:15
  • 1
    @Selcuk i have started a new question as i found that is is a bit ambiguous with typed and other things in python and could not find answer in duplicates tagged. It would be helpful if you suggest something there. Great to connect with you – Aagam Sheth Nov 19 '20 at 07:17

0 Answers0