🐍

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.

Study this deck free

Try the first 10 cards

1 / 10
Question

Class in Python

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

Already have an account? Sign in

Cards

  1. 1Class in Python
    A blueprint for creating objects that bundles data (attributes) and functionality (methods) together. Defined with 'class' keyword.
  2. 2Define a class
    class ClassName: def __init__(self, param): self.attribute = param
  3. 3Instance
    An object created from a class. Each instance has its own data but shares methods with other instances of the same class.
  4. 4Self
    Reference to the current instance. First parameter of instance methods. Allows accessing attributes and methods. Convention, not a keyword.
  5. 5__init__
    Constructor method called when creating a new instance. Initializes instance attributes. Example: def __init__(self, name): self.name = name
  6. 6Instance attributes
    Variables unique to each instance. Defined in __init__ or other methods. Accessed via self.attribute_name.
  7. 7Class attributes
    Variables shared by all instances of a class. Defined directly in class body, not in methods. Example: class Dog: species = 'Canis familiaris'
  8. 8instance vs class attributes
    Instance attributes are unique per instance (self.attr). Class attributes are shared across all instances (ClassName.attr). Instance lookup checks instance first, then class.
  9. 9Instance methods
    Functions defined in a class that operate on instance data. Take self as first parameter. Example: def bark(self): return f'{self.name} says woof!'
  10. 10Class methods
    Methods that operate on class itself, not instances. Decorated with @classmethod. Take cls as first parameter. Example: @classmethod def from_string(cls, string):
  11. 11Static methods
    Methods that don't access instance or class. Decorated with @staticmethod. No self/cls parameter. Utility functions grouped with class.
  12. 12Inheritance
    Mechanism allowing a class to inherit attributes and methods from another class. Child class extends parent. Syntax: class Child(Parent):
  13. 13Method overriding
    Child class redefines a parent method. When called on child instance, child's version executes. Can call parent with super().
  14. 14Super()
    Built-in function to call parent class methods. Common in __init__: super().__init__(args). Handles multiple inheritance correctly.
  15. 15Multiple inheritance
    Class inherits from multiple parents. Syntax: class Child(Parent1, Parent2). Method resolution follows MRO (Method Resolution Order).
  16. 16MRO (Method Resolution Order)
    Order Python searches for methods in inheritance hierarchy. View with ClassName.mro() or ClassName.__mro__. Uses C3 linearization.
  17. 17Polymorphism
    Different classes can be used through same interface. Objects of different classes respond to same method call differently. Duck typing in Python.
  18. 18Encapsulation
    Bundling data and methods together in a class. Controlling access to internal state. Use properties and private attributes.
  19. 19Private attributes in Python
    By convention, prefix with underscore: _private or __private. __name triggers name mangling (_ClassName__name). Not truly private, just a convention.
  20. 20Name mangling
    __attribute becomes _ClassName__attribute. Helps avoid name conflicts in inheritance. Not for security, just convention.
  21. 21@property
    Decorator making a method accessible as an attribute. Allows getter/setter logic while maintaining attribute syntax. Example: @property def name(self): return self._name
  22. 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. 23__str__
    Magic method defining string representation for humans. Called by str() and print(). Example: def __str__(self): return f'{self.name}'
  24. 24__repr__
    Magic method defining string representation for developers. Should be unambiguous, ideally valid Python code. Called by repr(). Fallback for __str__.
  25. 25__str__ vs __repr__
    __str__: readable for end users. __repr__: unambiguous for developers, debugging. __repr__ is fallback if __str__ missing.
  26. 26__len__
    Defines behavior for len(). Example: def __len__(self): return len(self.items)
  27. 27__getitem__
    Enables indexing/slicing: obj[key]. Example: def __getitem__(self, index): return self.items[index]
  28. 28__setitem__
    Enables item assignment: obj[key] = value. Example: def __setitem__(self, key, value): self.items[key] = value
  29. 29__eq__
    Defines equality comparison with ==. Example: def __eq__(self, other): return self.id == other.id
  30. 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
Study all 59 cards free