Skip to content
Advertisement

What for we call for the typing import List from the Python Standard Library?

I am solving some questions from the Leetcode: https://leetcode.com/problems/remove-duplicates-from-sorted-array/

I find the answers: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length

The answer for this question can be, too:

    class Solution():
        def removeDuplicates(self, nums):
            i = 0
            while i < len(nums)-1:
                if nums[i] == nums[i+1]:
                    del nums[i]
                else:
                    i += 1
   
           return (len(nums), nums)

    numbers = [1, 1, 1, 3, 4, 5, 5, 5, 5, 7, 7, 7, 9, 9]
    some_object = Solution()
    ask_me = some_object.removeDuplicates(numbers)
    print (ask_me)

I can’t find the answer to the question of why in the code should be applied typing. On the other hand, the following code work very well, too:

    from typing import List

    class Solution(object):
        def removeDuplicates(self, nums: List[int]) -> int:
            i = 0
            while i < len(nums)-1:
                if nums[i] == nums[i+1]:
                    del nums[i]
                else:
                    i += 1

            return (len(nums), nums)


    numbers = [1, 1, 1, 3, 4, 5, 5, 5, 5, 7, 7, 7, 9, 9]
    some_object = Solution()
    ask_me = some_object.removeDuplicates(numbers)
    print (ask_me)

So please, I would like to clarify every aspects of the above codes. Maybe somebody can help me and other interested and clarify us what for we call for the typing import List from the Python Standard Library?

Advertisement

Answer

Programming languages are either statically typed or dynamically typed.

In languages with static typing like java or c++, the type of the variable must be known at compile-time. If we declare a variable, it should be known (or inferrable) by the compiler if it will be a number, a string, or a boolean. Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running).

Python is dynamically typed, typed of a variable is known only when running the program. Adding typing in python does not affect the performance. You can write your code without a type annotation, it will run. Typing just helps you to write cleaner code which is a very important aspect. Programmers are like authors, you have to write better and cleaner code, so other programmers can easily understand your code. It helps you too because in the future if you need to revisit your code, it will help you to understand.

In tech companies, not a single person maintains the code, there is a team that constantly reads and maintains the code.

I forgot to mention. when you add type annotation, your idee will show you available methods. let’s say you annotate

 my_variable:str

when you type this variable, “my_variable.” after dot, your IDE will show you all string methods.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement