───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───
Classes
- A layout for creating objects
- Contains attributes (data) & methods (functions)
- Attributes & methods are accessed within the class using the
self
keyword in Python
Methods
- Functions that are inside of a class
- Can access the data of the class via
self
Constructors
- Function that is ran when the class is instantiated
- In Python, defined with the
__init__
method- C# defines with the name of the class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello():
print(f"Hello {self.name}!")
Data Classes
- A special type of class to store data
- Created with the
@dataclass
decorator- This automatically creates the
__repr__
method
- This automatically creates the
- Equality based on data rather than object identity
- Simple, immutable data structures typically without behavior (although you can add methods)
from dataclasses import dataclass
@dataclass # or @dataclass(frozen=True) for immutability
class Point:
x: int
y: int
# without data classes
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})"
───✱*.。:。✱*.:。✧*.。✰*.:。✧*.。:。*.。✱ ───