Zum Hauptinhalt springen
Intelligence View
⚡ tsecurity.de Intelligence

Python for Beginners — Part 8: Object-Oriented Python

Part 8 of an 8-part beginner-friendly series on learning Python from scratch. Congratulations — you've made it to the final part! Everything you've learned — va…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Quelle & Fakten · 8 Min. Lesezeit
Quelle: dev.to
Rubrik: Programmation sécurisée

Part 8 of an 8-part beginner-friendly series on learning Python from scratch.



Congratulations — you've made it to the final part!



Everything you've learned — variables, control flow, collections, functions, file I/O, error handling — can be organized into classes and objects. This is object-oriented programming (OOP), and it's how professional Python code is written.



OOP isn't required to write working programs, but it's essential for writing code that scales, is maintainable, and professional. This final article brings everything together.






Classes and Objects



A class is a blueprint for creating objects. An object is an instance of that class. Think of a class like a cookie cutter and objects like the cookies it produces.






Defining a class






class Person:
pass

# Create an object (instance)
person = Person()
print(person) # <__main__.Person object at 0x...>






class Person: defines a class. person = Person() creates an object from that class.






The __init__ Method and self



The __init__ method runs automatically when you create an object. It's where you initialize the object's data.




class Person:
def __init__(self, name, age):
self.name = name
self.age = age

person = Person("Ramesh", 25)
print(person.name) # Ramesh
print(person.age) # 25






self represents the object itself. When you write self.name = name, you're saying "this object's name attribute equals the name I was given."



Think of it this way:





  • name (parameter) is the input


  • self.name (attribute) is the object's stored data






Creating multiple objects






class Person:
def __init__(self, name, age):
self.name = name
self.age = age

person1 = Person("Ramesh", 25)
person2 = Person("Priya", 23)

print(person1.name, person1.age) # Ramesh 25
print(person2.name, person2.age) # Priya 23






Each object is independent. person1.name is separate from person2.name.






Methods



A method is a function inside a class. Methods operate on the object's data:




class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
return f"Hello, I'm {self.name}"

def have_birthday(self):
self.age += 1

person = Person("Ramesh", 25)
print(person.greet()) # Hello, I'm Ramesh
person.have_birthday()
print(person.age) # 26






Call methods with dot notation: person.greet(). The self parameter is passed automatically — you don't type it.






Instance vs Class Variables



Instance variables are unique to each object. Class variables are shared by all objects of that class:




class Dog:
species = "Canis familiaris" # Class variable

def __init__(self, name):
self.name = name # Instance variable

dog1 = Dog("Buddy")
dog2 = Dog("Max")

print(dog1.name) # Buddy (unique to dog1)
print(dog2.name) # Max (unique to dog2)
print(Dog.species) # Canis familiaris (shared)
print(dog1.species) # Canis familiaris (accessible via instance too)






Use class variables for data shared by all instances. Use instance variables for data unique to each instance.






Inheritance



Inheritance lets you create a new class based on an existing class. The new class inherits methods and attributes from the parent:




class Animal:
def __init__(self, name):
self.name = name

def speak(self):
return f"{self.name} makes a sound"

class Dog(Animal):
def speak(self):
return f"{self.name} barks"

class Cat(Animal):
def speak(self):
return f"{self.name} meows"

dog = Dog("Buddy")
cat = Cat("Whiskers")

print(dog.speak()) # Buddy barks
print(cat.speak()) # Whiskers meows






Dog and Cat inherit from Animal and override the speak() method. This is called method overriding.






Using super()



Call the parent class's method with super():




class Animal:
def __init__(self, name):
self.name = name

def describe(self):
return f"{self.name} is an animal"

class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call parent's __init__
self.breed = breed

def describe(self):
parent_desc = super().describe() # Call parent's describe
return f"{parent_desc} and is a {self.breed}"

dog = Dog("Buddy", "Golden Retriever")
print(dog.describe()) # Buddy is an animal and is a Golden Retriever






super() lets you access the parent class without repeating code.






Polymorphism



Polymorphism means "many shapes" — the same function name works differently depending on the object type:




class Dog:
def speak(self):
return "Woof!"

class Cat:
def speak(self):
return "Meow!"

class Cow:
def speak(self):
return "Moo!"

animals = [Dog(), Cat(), Cow()]

for animal in animals:
print(animal.speak())

# Output:
# Woof!
# Meow!
# Moo!






Each animal speaks differently, but you call the same method. This is polymorphism in action.






Encapsulation



Encapsulation is the idea of bundling data and methods together, and controlling access to them. Python uses naming conventions:





  • public_attribute — accessible from outside (convention)


  • _private_attribute — intended private (convention, not enforced)


  • __really_private — name-mangled, harder to access (enforced)




class BankAccount:
def __init__(self, balance):
self.__balance = balance # "Private"

def deposit(self, amount):
if amount > 0:
self.__balance += amount

def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount

def get_balance(self):
return self.__balance

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance()) # 1500

# Can't access directly:
# print(account.__balance) # AttributeError (name-mangled)






The double underscore (__) makes the attribute private. Access it through methods instead. This prevents accidental misuse.






Dunder Methods



Dunder methods (double underscore) are special methods Python calls automatically. Here are the most important:






__str__ and __repr__






class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __str__(self):
return f"{self.name} ({self.age})"

def __repr__(self):
return f"Person('{self.name}', {self.age})"

person = Person("Ramesh", 25)
print(str(person)) # Ramesh (25) — human-readable
print(repr(person)) # Person('Ramesh', 25) — for debugging






__str__ is for end users. __repr__ is for developers.






__len__






class ShoppingCart:
def __init__(self):
self.items = []

def add(self, item):
self.items.append(item)

def __len__(self):
return len(self.items)

cart = ShoppingCart()
cart.add("apple")
cart.add("banana")
print(len(cart)) # 2






Now len(cart) returns the number of items.






__eq__






class Person:
def __init__(self, name):
self.name = name

def __eq__(self, other):
return self.name == other.name

person1 = Person("Ramesh")
person2 = Person("Ramesh")
print(person1 == person2) # True (compares names)






Define how == works for your objects.






Properties and Getters/Setters



Properties let you access attributes like data, but with method-like control:




class Person:
def __init__(self, name):
self._name = name

@property
def name(self):
return self._name

@name.setter
def name(self, value):
if len(value) > 0:
self._name = value

person = Person("Ramesh")
print(person.name) # Ramesh (getter)
person.name = "Priya" # Setter
print(person.name) # Priya






The @property decorator makes name act like an attribute but run validation code.






Practical Examples






Example 1: User class






class User:
def __init__(self, username, email):
self.username = username
self.email = email
self.created_at = datetime.now()

def __str__(self):
return f"User({self.username})"

def change_email(self, new_email):
if "@" in new_email:
self.email = new_email
else:
raise ValueError("Invalid email")

user = User("ramesh", "[email protected]")
print(user) # User(ramesh)
user.change_email("[email protected]")









Example 2: Inheritance with Bank Accounts






class Account:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance

def deposit(self, amount):
self.balance += amount

class SavingsAccount(Account):
def __init__(self, owner, balance, interest_rate):
super().__init__(owner, balance)
self.interest_rate = interest_rate

def apply_interest(self):
self.balance *= (1 + self.interest_rate)

savings = SavingsAccount("Ramesh", 1000, 0.05)
savings.deposit(500)
savings.apply_interest()
print(savings.balance) # 1575.0









Example 3: Polymorphism with shapes






class Shape:
def area(self):
raise NotImplementedError

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius ** 2

class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(f"Area: {shape.area()}")

# Output:
# Area: 78.5
# Area: 24









Why This Matters



OOP is how professional code is organized. Without it, programs become tangled messes of functions and variables. With OOP, you group related data and behavior together into classes, making code:





  • Reusable — inheritance lets you extend existing code


  • Maintainable — encapsulation hides complexity


  • Scalable — polymorphism lets you add new types easily


  • Clear — objects represent real-world concepts



The most common beginner mistakes:




  • Forgetting self in methods

  • Confusing __init__ (constructor) with class definition

  • Not using inheritance when it makes sense

  • Overcomplicating with too many classes (start simple)

  • Breaking encapsulation with direct attribute access






Your Journey is Complete



You've now learned:





  1. Syntax and basics (Parts 1–2)


  2. Working with text and logic (Parts 3–4)


  3. Organizing data (Part 5)


  4. Organizing code (Part 6)


  5. Working in the real world (Part 7)


  6. Professional code structure (Part 8: OOP)



You have all the fundamental skills of a Python programmer. Where you go from here is up to you — web development, data science, automation, games, machine learning — Python is everywhere.






What's Next



You've completed the 8-part beginner series. From here:





  • Web development: Flask or Django frameworks


  • Data science: Pandas, NumPy, Matplotlib


  • Automation: Scripts to automate tasks


  • Games: Pygame library


  • Machine learning: Scikit-learn, TensorFlow


  • Advanced Python: Decorators, metaclasses, async/await



The fundamentals you've learned here apply to everything. Pick a project that excites you and build it.

Social ReaktionenReagiere als Erste:r — dein Feedback zählt!
Informations-Bewertung:
✏️ Information ergänzen
🛡️ Versioniert via CMS-Revisions-Engine (Freigabe-Queue für Gäste)

💬 Discussion communautaire et commentaires

🌐 Rejoindre le chat principal

Lancez la discussion dans le flux diffusé du Social Chat ou proposez ci-dessus une contribution experte via le formulaire de complément.

Briefing sécurité Gratuit · désabonnement en 1 clic

Intelligence Digest — gratuit

Chaque jour, les principales actualités sécurité · résiliable à tout moment

Évaluation communautaire &amp; preuve sociale
⭐ Avis des lecteurs
–
Ø / 5.0
Pas encore d'évaluation des lecteurs
Cet article vous a-t-il été utile ?
0
Donner votre évaluation
Évaluation détaillée (facultatif)
Contenu & qualité
Source & fiabilité
Actualité

Groupes d’histoires apparentées & sources (IA vectorielle)

6 Cluster
27 sources
Black Friday 2026 laptop deals: top offers on Chromebooks, MacBooks, Windows 11 laptops and more
Latest from TechRadar il y a 38 min.
26 autres sources
CNETil y a 1 hThis Black Friday Deal Will Save You $310 on a Chefman Indoor Pizza Oven netzwelt.deil y a 1 hBetrugsanruf oder echter Paketdienst? So schützt ihr euch beim Online-Shopping - zum Black Friday spart ihr 75 Prozent bei Norton 360 Advanced netzwelt.deil y a 38 min.Den besten Telekom-Tarif am Black Friday gibt es wieder, aber es gibt eine Haken PC-WELTil y a 30 min.Amazon Black Friday Woche: Die besten Technik-Deals im Überblick Latest from TechRadaril y a 56 min.Newegg knocks $200 off the 4TB KingSpec P3 SATA SSD for Black Friday CNETil y a 19 joursGTA 6 May Not Be on Sale, but These Black Friday PlayStation Deals Are Still Worth Shopping The Vergeil y a 38 min.Dreame and Eufy are offering nearly 60 percent off robovacs for Black Friday Latest from TechRadaril y a 1 hBlack Friday Roku deals: save on Roku Streaming Sticks, Roku TVs, and more Computer Bildil y a 1 hBigbank erhöht Festgeldzinsen: Bis zu 3,60 Prozent zum Black Friday Golem.deil y a 1 hAnzeige: 65 Zoll großer OLED-TV von Samsung günstig wie nie am Black Friday Latest from TechRadaril y a 3 hBlack Friday Pixel deals: save big on Pixel 10, Pixel 10a, Pixel 11 Pro, and more Latest from TechRadaril y a 2 hSamsung's Black Friday sale is here — I've found 11 excellent deals, including up to $1,800 off OLED TVs, Galaxy S26 Ultra, tablets, and more Latest from TechRadaril y a 2 hPick your favourite shade: every AeroPress colour is on sale for Black Friday Latest from TechRadaril y a 2 hPureVPN has launched 5-year plans just in time for Black Friday — $1.49 per month for half a decade of protection Latest from TechRadaril y a 1 hDDR5 RAM Black Friday deals: 6 32GB kits under $500 — because sadly that’s where memory prices are right now Latest from TechRadaril y a 1 h25 hand-picked deals in the Amazon UK Black Friday sale — Lego, Bose earbuds, Switch 2, Ninja Crispi, Kindles, and more WIREDil y a 1 hThe 24 Very Best Amazon Black Friday Deals to Get Now (2026) heise online Newsil y a 1 jourBlack Friday 2026: Die besten Deals für Smartphones, Tablets und Smartwatches WIREDil y a 1 hBest Black Friday Nintendo Switch 2 Deals (2026): Bundles, Games Latest from Windows Centralil y a 2 hDell's budget-friendly XPS 13 Windows laptop drops below $550 ahead of Black Friday PC-WELTil y a 1 h575 Euro günstiger: Solakon onPower Balkonkraftwerk mit ONE-Speicher im Black-Friday-Deal PC-WELTil y a 1 hBlack Friday bei PC-WELT: Norton 360 Premium-Security nur 19,99 Euro PC-WELTil y a 1 h575 Euro günstiger: Solakon onPower Balkonkraftwerk mit ONE-Speicher im Black-Friday-Deal PC-WELTil y a 1 hBlack Friday bei PC-WELT: Norton 360 Premium-Security nur 19,99 Euro PC-WELTil y a 2 hBlack Friday: Diese Handytarife sind jetzt besonders günstig – von 10 GB bis Unlimited PC-WELTil y a 2 hBlack Friday: Diese Handytarife sind jetzt besonders günstig – von 10 GB bis Unlimited
Articles similaires
🔍 Verwandte News

Auch interessante Nachrichten Python for Beginners — Part 8: Object-Oriented Python

Thematisch verwandte Begriffe: Python, Beginners, Part, ObjectOriented · 6 Treffer

Laden...

Chargement des vidéos ...

Laden...

Chargement des articles ...

Laden...

Chargement des vidéos ...

Laden...

Chargement des articles ...

Laden...

Chargement des vidéos ...

Laden...

Chargement des articles ...

Laden...

Chargement des vidéos ...

Laden...

Chargement des articles ...

Laden...

Chargement des vidéos ...

💬 Chargement des commentaires…
Sentinel CTI: ONLINE
Tirer pour actualiser
Widgets & Topics
Loading widgets…
Nächster Beitrag