In programming, a language is considered "dynamically typed" when the type of a variable is checked during runtime rather than in advance (at compile time). This means that you do not need to explicitly specify the type of a variable when you declare it. Instead, the type of the variable is determined at runtime based on the value assigned to it.
Type Flexibility: You can assign any type of value to a variable, and you can change the type of the variable by assigning a new value of a different type.
어떤 타입의 값이든 변수에 할당할 수 있고 새로운 타입의 값을 해당 변수에 할당해서 그 변수의 타입을 바꿀수 있음
Ease of Use: Generally, dynamically typed languages are easy to write and read because they do not require detailed type declarations.
일반적으로 동적타입은 읽고쓰기 쉬운데, 왜냐하면 구체적인 타입선언이 필요하지 않기 때문임.
Runtime Type Checking: Errors related to types are checked during execution, which can lead to runtime errors if not properly handled.
타입과 관련된 에러가 실행과 함께 체크돼서 런타임 에러가 발생할 수 있음
Python is a dynamically typed language. Here's an example to illustrate how Python handles variable types dynamically:
# Dynamically typed variables
x = 10 # Initially, x is an integer
print(type(x)) # Outputs: <class 'int'>
x = "Hello" # Now, x is a string
print(type(x)) # Outputs: <class 'str'>
x = 3.14 # Now, x is a floating-point number
print(type(x)) # Outputs: <class 'float'>
In this example, the variable x
is first assigned an integer, then a string, and finally a floating-point number. The type of x
changes dynamically based on the value assigned to it, demonstrating Python’s flexible and dynamic type system.
This flexibility allows developers to work more quickly and with fewer upfront declarations. However, it also means that type-related errors can occur at runtime, which might not be detected until the specific piece of code is executed. Therefore, dynamic typing requires careful handling of variables and data types, especially in large and complex applications.
A generic function is a function that is defined in terms of types that are specified later when the function is actually used. This concept is often associated with type parameters in function definitions, allowing the function to operate on a variety of data types without being rewritten for each type. In programming languages that support generics, such as Java, C#, and others, generic functions provide a way to write more reusable and maintainable code.