A class in Python is a blueprint for creating objects. It allows grouping of data (attributes) and behavior (methods) together to model real-world entities.
Think of a class as a design template. Objects are created based on this template, each with their own values.
Concept | Description | Real-Time Example |
---|---|---|
Class |
Blueprint for an object | class Car: |
Object |
Instance of a class | my_car = Car() |
__init__() |
Constructor method | Initialize attributes |
self |
Refers to current object | Used in all methods |
Attribute |
Variable stored in object | self.name |
Method |
Function inside a class | def greet(self): |
Inheritance |
Use code from another class | class Manager(Employee): |
Encapsulation |
Hide internal details | _salary , __bonus |
Polymorphism |
Same method, different behavior | calculate_salary() overridden |
class Employee:
def __init__(self, name, role):
self.name = name
self.role = role
def greet(self):
return f"Hello, I am {self.name} and I work as a {self.role}"
# Creating object
emp = Employee("Sairam", "Data Engineer")
print(emp.greet())
Imagine building an employee management system. Each employee has a name, salary, and department. We want to give them raises and access info:
class Employee:
def __init__(self, name, salary, dept):
self.name = name
self.salary = salary
self.dept = dept
def apply_raise(self, percent):
self.salary += self.salary * (percent / 100)
return self.salary
# Example usage
e1 = Employee("John", 50000, "IT")
print(e1.apply_raise(10)) # New salary: 55000
Feature | Explanation |
---|---|
__init__() |
Constructor for setting up data |
self |
Represents current object |
Methods | Functions defined inside class |
Object | Created instance of class |
Inheritance | Child class can use Parent class methods |
Encapsulation | Hide data inside private variables |
Polymorphism | Reuse method name with different behavior |
import boto3 import pandas as pd import io class S3ToS3: def __init__(self, aws_access_key_id, aws_secret_access_key, region_name, source_bucket, source_key, destination_bucket, destination_key): self.aws_access_key_id = aws_access_key_id self.aws_secret_access_key = aws_secret_access_key self.region_name = region_name self.source_bucket = source_bucket self.source_key = source_key self.destination_bucket = destination_bucket self.destination_key = destination_key self.s3_client = boto3.client('s3', aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key, region_name=self.region_name) self.df = None def read_data_from_s3(self): response = self.s3_client.get_object(Bucket=self.source_bucket, Key=self.source_key) content = response['Body'].read().decode('utf-8') self.df = pd.read_csv(io.StringIO(content)) def tranform_data(self): self.df = self.df[['employee_id', 'name']] def load_to_s3(self): csv_buffer = io.StringIO() self.df.to_csv(csv_buffer, index=False) self.s3_client.put_object(Bucket=self.destination_bucket, Key=self.destination_key, Body=csv_buffer.getvalue()) if __name__ == '__main__': obj = S3ToS3(aws_access_key_id, aws_secret_access_key, region_name, source_bucket, source_key, destination_bucket, destination_key) print(obj.source_key) obj.read_data_from_s3() obj.tranform_data() obj.load_to_s3()