Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Comparing Classes and Dictionaries | Introduction à la POO en Python
Programmation Orientée Objet en Python

Comparing Classes and Dictionaries

Glissez pour afficher le menu

A dictionary groups data with key-value pairs, focusing only on storing information in a simple structure. A class, on the other hand, is a blueprint for creating objects, bundling both data (attributes) and behaviors (methods) to define them together. For example, a dictionary might hold a pet's name, breed, and age, but it's still just a collection of key-value pairs without any inherent behavior.

The core problem of a dictionary is that it only stores data without providing any behavior.

12345678910111213
pet_dict = {"name": "Buddy", "age": 3} class Pet: def __init__(self, name, age): self.name = name self.age = age def speak(self): return f"{self.name} says woof!" my_dog = Pet("Buddy", 3) print(pet_dict["name"]) print(my_dog.speak())

The class bundles both the data and the speak method, demonstrating how classes provide structure for organizing attributes and behavior together.

question mark

Which statement best explains the difference between a dictionary and a class?

Sélectionnez la réponse correcte

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Section 1. Chapitre 2
some-alt