Python: OOP Fundamentals
59 cards · 🇺🇸 English
Classes, inheritance, and core object-oriented Python concepts.
Study all 59 cards for free
Flashcards, quizzes, pixel-art games and a daily review that remembers for you.
Try the first 10 cards
1 / 10Class in Python
Tap the card for the answer · swipe right if you knew it, left if not
Already have an account? Sign in
Cards
- 1Class in PythonA blueprint for creating objects that bundles data (attributes) and functionality (methods) together. Defined with 'class' keyword.
- 2Define a classclass ClassName: def __init__(self, param): self.attribute = param
- 3InstanceAn object created from a class. Each instance has its own data but shares methods with other instances of the same class.
- 4SelfReference to the current instance. First parameter of instance methods. Allows accessing attributes and methods. Convention, not a keyword.
- 5__init__Constructor method called when creating a new instance. Initializes instance attributes. Example: def __init__(self, name): self.name = name
- 6Instance attributesVariables unique to each instance. Defined in __init__ or other methods. Accessed via self.attribute_name.
- 7Class attributesVariables shared by all instances of a class. Defined directly in class body, not in methods. Example: class Dog: species = 'Canis familiaris'
- 8instance vs class attributesInstance attributes are unique per instance (self.attr). Class attributes are shared across all instances (ClassName.attr). Instance lookup checks instance first, then class.
- 9Instance methodsFunctions defined in a class that operate on instance data. Take self as first parameter. Example: def bark(self): return f'{self.name} says woof!'
- 10Class methodsMethods that operate on class itself, not instances. Decorated with @classmethod. Take cls as first parameter. Example: @classmethod def from_string(cls, string):
- 11Static methodsMethods that don't access instance or class. Decorated with @staticmethod. No self/cls parameter. Utility functions grouped with class.
- 12InheritanceMechanism allowing a class to inherit attributes and methods from another class. Child class extends parent. Syntax: class Child(Parent):
- 13Method overridingChild class redefines a parent method. When called on child instance, child's version executes. Can call parent with super().
- 14Super()Built-in function to call parent class methods. Common in __init__: super().__init__(args). Handles multiple inheritance correctly.
- 15Multiple inheritanceClass inherits from multiple parents. Syntax: class Child(Parent1, Parent2). Method resolution follows MRO (Method Resolution Order).
- 16MRO (Method Resolution Order)Order Python searches for methods in inheritance hierarchy. View with ClassName.mro() or ClassName.__mro__. Uses C3 linearization.
- 17PolymorphismDifferent classes can be used through same interface. Objects of different classes respond to same method call differently. Duck typing in Python.
- 18EncapsulationBundling data and methods together in a class. Controlling access to internal state. Use properties and private attributes.
- 19Private attributes in PythonBy convention, prefix with underscore: _private or __private. __name triggers name mangling (_ClassName__name). Not truly private, just a convention.
- 20Name mangling__attribute becomes _ClassName__attribute. Helps avoid name conflicts in inheritance. Not for security, just convention.
- 21@propertyDecorator making a method accessible as an attribute. Allows getter/setter logic while maintaining attribute syntax. Example: @property def name(self): return self._name
- 22Create a setter with @property@property for getter, then @attribute.setter for setter. Example: @property def name(self): return self._name @name.setter def name(self, value): self._name = value
- 23__str__Magic method defining string representation for humans. Called by str() and print(). Example: def __str__(self): return f'{self.name}'
- 24__repr__Magic method defining string representation for developers. Should be unambiguous, ideally valid Python code. Called by repr(). Fallback for __str__.
- 25__str__ vs __repr____str__: readable for end users. __repr__: unambiguous for developers, debugging. __repr__ is fallback if __str__ missing.
- 26__len__Defines behavior for len(). Example: def __len__(self): return len(self.items)
- 27__getitem__Enables indexing/slicing: obj[key]. Example: def __getitem__(self, index): return self.items[index]
- 28__setitem__Enables item assignment: obj[key] = value. Example: def __setitem__(self, key, value): self.items[key] = value
- 29__eq__Defines equality comparison with ==. Example: def __eq__(self, other): return self.id == other.id
- 30Comparison magic methods__eq__ (==), __ne__ (!=), __lt__ (<), __le__ (<=), __gt__ (>), __ge__ (>=). Define how objects compare.
And 29 more cards. Study the whole deck free
Articles about this deck
More Programming decks
See all- 🐍
Python : avancé (décorateurs, lambda et exceptions)
97 cards · 🇺🇸 English
Décorateurs, fonctions lambda, gestion des exceptions et autres concepts Python avancés.
- 🐍
Python : structures de données et compréhensions
125 cards · 🇺🇸 English
Méthodes de listes/dictionnaires/ensembles ainsi que les compréhensions de listes, de dictionnaires et de générateurs.
- 🐍
Python: Fortgeschritten (Decorators, Lambda & Exceptions)
97 cards · 🇺🇸 English
Decorators, Lambda-Funktionen, Exception-Handling und weitere fortgeschrittene Python-Konzepte.
- 🐍
Python: Funciones integradas
77 cards · 🇺🇸 English
Funciones integradas esenciales que todo desarrollador Python debería conocer.
- 🐍
Python : fondamentaux de la POO
59 cards · 🇺🇸 English
Classes, héritage et concepts fondamentaux de la programmation orientée objet en Python.
- 🐍
Python: Fundamentos de POO
59 cards · 🇺🇸 English
Clases, herencia y conceptos fundamentales de la programación orientada a objetos en Python.
