Python is an object-oriented programming language that provides various ways to define and use classes.
python classes
A python class is a blueprint for creating instances / objects. A normal class is a user-defined class that doesn't include any special attributes or methods. Normal classes use a constructor method, __init__
that initializes the class instance with default or provided inputs.
here's an example:
class Dog:
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
data classes
a data class is a special class created using the @dataclass decorator. they are meant to be used for simple, immutable data structures, sans behavior.
here's an example of.a nifty little data class in Python:
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
normal & data classes: differences
brevity:
Data classes are more concise than normal classes, as they require much less code to define. The parameters are automatically generated, and a __repr__
is provided for us.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
@dataclass
class Point2:
x: int
y: int
Writing attributes is a lot quicker too in a data class. Let's have a look below.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_age(self):
return self.age
def set_age(self, age):
self.age = age
Here, we created a normal class named Person and defined two attributes, name and age using a couple pairs of setter and getter functions.
Now, let's create a data class with two attributes.
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
Super brief, super legible, super cool!
mutability:
Data classes are immutable by default, while normal classes are mutable.
Which means after instances of data classes are made, their attributes can't be edited.
Is it good to have an object that can't be edited? Like most things in life, it depends.
type-hinting:
Type hinting is another area where data classes and normal classes differ. With data classes, we can specify the types of our attributes in the class definition. This is done using Python's built-in type annotations. Here's an example:
from typing import List
@dataclass
class Person:
name: str
age: int
hobbies: List[str]
As you can see, we specified the types of the name, age, and hobbies attributes. This allows Python's type checker to catch any type errors during development, which can help prevent bugs and improve the reliability of our code.
In contrast, with normal classes, we have to manually specify the types of our attributes in the constructor or in the class definition. Here's an example:
from typing import List
class Person:
def __init__(self, name: str, age: int, hobbies: List[str]):
self.name = name
self.age = age
self.hobbies = hobbies
equality:
By default, data classes define equality based on the values of their attributes, meaning that two instances of a data class are considered equal if all their attributes have the same values.
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
p1 = Person("Alice", 30)
p2 = Person("Alice", 30)
print(p1 == p2) # True
In contrast, normal classes define equality based on object identity, meaning that two instances of a normal class are considered equal only if they are the same object in memory.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Alice", 30)
p2 = Person("Alice", 30)
print(p1 == p2) # False
Wow!
conclusion
we use both data and normal classes in python to represnt objects. While data classes are more concise and legible, normal classes give us much more versatility.
Top comments (0)