Search

Python 클래스 변수 타입선언만 해논건 왜 되는거지?

Why python class var with only annotation works?
In Python, when you declare a class variable with only an annotation but without an assignment, it does not actually create a variable. The annotation is stored in the class's __annotations__ dictionary, but the variable is not added to the class's __dict__ dictionary. This means you cannot access the variable directly as you would with a normal class variable.
Here is an example:
class MyClass: my_var: int print(MyClass.__annotations__) # Outputs: {'my_var': <class 'int'>} print(MyClass.__dict__) # Does not contain 'my_var' print(MyClass.my_var) # Raises AttributeError: type object 'MyClass' has no attribute 'my_var'
Python
복사
This behavior is part of Python's type hinting system, which is used to annotate the expected types of variables, function parameters, and return values. Type hints are not enforced by the Python interpreter, but they can be used by static type checkers, linters, and IDE features to help catch potential errors.
In the example above, my_var: int is a type hint indicating that my_var is intended to be an integer. However, since no actual value is assigned to my_var, it doesn't exist as a variable in the class.
If you want to create a class variable with a type hint and an initial value, you can do so by assigning a value to the variable:
class MyClass: my_var: int = 0 # Now my_var exists and has an initial value of 0 print(MyClass.my_var) # Outputs: 0
Python
복사
In this example, my_var is a class variable with a type hint of int and an initial value of 0.

What is the benefit of using dataclass on python?

Python's dataclasses module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes. Here are a few benefits of using dataclasses:
1.
Reduced Boilerplate Code: Dataclasses automatically generate boilerplate code for you. For example, instead of writing a lengthy __init__() method to initialize all the attributes, you just need to list the attributes in the class definition and the @dataclass decorator will automatically generate the __init__() method for you.
from dataclasses import dataclass @dataclass class Point: x: int y: int
Python
복사
1.
Generated Methods: Besides __init__(), dataclasses also generate other useful special methods such as __repr__() for easy debugging and __eq__() for comparing objects.
2.
Immutability: Dataclasses support immutability with the frozen parameter. If @dataclass(frozen=True) is used, it makes the generated class immutable and hashable.
3.
Type Checking: Dataclasses use type hints, which not only make the code more readable and self-documenting, but also enable tools to provide better support for static type checking, linting, and IDE features.
4.
Efficiency: They can make the code more efficient by reducing the amount of code you need to write, making it more readable and easier to maintain.
5.
Default Values: Dataclasses support default values and default factories for attributes.
Remember, dataclasses were introduced in Python 3.7, so if you're using an older version of Python, you'll need to install the dataclasses module with pip (pip install dataclasses).