1. Home
  2. Explore
  3. Computer Science
  4. Python OOP Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python OOP Course

  • 40Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Basic OOP Concepts
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a class in Python?

Tap to flipClick to flipspace

A blueprint for creating custom data types. It defines attributes and methods that its instances (objects) will have.

Or swipe the card

40 cards

  1. What is a class in Python?

    A blueprint for creating custom data types. It defines attributes and methods that its instances (objects) will have.

  2. How do you create an instance of a class in Python?

    By calling the class name like a function: item1 = Item()

  3. How do you assign attributes to an instance?

    Using dot notation after the instance: item1.name = "Phone"

  4. What is a method in a Python class?

    A function defined inside a class that is meant to be called on an instance. The first parameter is always self, representing the instance itself.

  5. What is the self parameter in Python methods?

    It is the first parameter of instance methods, representing the instance on which the method is called. By convention, it is named self.

  6. What error occurs if you define a method without parameters and try to call it from an instance?

    TypeError: method() takes 0 positional arguments but 1 was given.

  7. What is the __init__ method in Python classes?

    A special method (constructor) that is automatically called when a new instance is created. It is used to initialize instance attributes.

  8. How can you enforce that certain attributes must be passed when creating an instance?

    By defining those attributes as parameters in the __init__ method and assigning them with self.attribute = parameter.

  9. What are instance attributes?

    Attributes that are unique to each instance, typically assigned in __init__ using self.attribute = value.

  10. What are class attributes?

    Attributes that belong to the class itself and are shared across all instances. They are defined directly inside the class body.

  11. How do you define a class attribute?

    By assigning a value inside the class body, outside any method. Example: pay_rate = 0.8 inside the class Item.

  12. What happens when you access an attribute on an instance that is not an instance attribute?

    Python looks for the attribute in the class attributes. If found, it returns that class attribute's value.

  13. How can you see all attributes of an instance or class?

    Using the built-in __dict__ attribute, e.g., instance.__dict__ returns a dictionary of instance attributes.

  14. How do you override a class attribute for a specific instance?

    Assign the attribute directly on that instance: instance.attribute = new_value. Then the instance will use its own copy.

  15. Why should you use self.pay_rate instead of Item.pay_rate in a method that applies a discount?

    To allow per-instance pay rates to take effect. self.pay_rate looks for an instance attribute first; if absent, it falls back to the class attribute.

  16. What is the difference between instance methods and class methods?

    Instance methods take self as first parameter and operate on instance data. Class methods take cls as first parameter and operate on class-level data.

  17. How do you define a class method?

    Use the @classmethod decorator above a method definition. The first parameter must be cls (by convention).

  18. What is a use case for a class method?

    Creating instances from structured data like CSV, JSON, or YAML files. They act as factory methods.

  19. What is a static method?

    A method that does not receive any automatic first argument (neither self nor cls). It behaves like a regular function but belongs to the class's namespace.

  20. What is an example of when to use a static method instead of a class method?

    A utility function that checks if a number is an integer (ignoring .0 floats), like is_integer(num). It doesn't need access to the class or instance.

  21. Can static methods and class methods be called from instances?

    Yes, but it is not recommended. It can cause confusion because they are designed to be called from the class level.

  22. What does the @classmethod decorator do?

    It converts a method into a class method, meaning the first argument passed automatically is the class itself (conventionally named cls).

  23. What does the @staticmethod decorator do?

    It converts a method into a static method, removing the automatic passing of self or cls. It behaves like a regular function inside the class.

  24. What is inheritance in OOP?

    A mechanism where a child class derives properties and behaviors from a parent class, allowing code reuse.

  25. How do you create a child class that inherits from a parent class?

    class ChildClass(ParentClass): inside the class definition. Example: class Phone(Item):.

  26. What is the super() function used for in Python?

    It returns a temporary object of the superclass, allowing you to call its methods (especially __init__) from the child class.

  27. What happens if a child class defines its own __init__ without calling super().__init__()?

    The parent's __init__ is not called, so parent attributes are not initialized unless explicitly assigned in the child's __init__.

  28. What warning does Python give if a child class defines __init__ without calling super().__init__()?

    A warning like 'Call to init of super class is missed'. It's a hint that you should call the parent's constructor.

  29. How can you make the __repr__ method show the child class name instead of hardcoding Item?

    Use self.__class__.__name__ to dynamically get the class name. Example: return f"{self.__class__.__name__}('{self.name}', {self.price}, {self.quantity})".

  30. What is encapsulation in OOP?

    The principle of restricting direct access to some of an object's attributes and methods, usually by making them private and providing controlled access via getters/setters.

  31. How do you create a read-only attribute in Python?

    Use the @property decorator on a method that returns the attribute's value. This allows access but prevents assignment (unless a setter is defined).

  32. How do you create a getter and setter for an attribute in Python?

    Define a property with @property (getter) and @attribute_name.setter (setter). The setter method receives the new value as a parameter.

  33. How do you make an attribute truly private in Python?

    Prefix the attribute name with double underscore (e.g., self.__name). This triggers name mangling, making it harder to access from outside the class.

  34. What is name mangling in Python?

    The process where an attribute with double underscore prefix (e.g., __name) is transformed to _ClassName__name to avoid accidental overriding in subclasses.

  35. What is the difference between single underscore _name and double underscore __name?

    Single underscore is a convention for 'protected' (internal use, but accessible). Double underscore triggers name mangling for 'private' (harder to access externally).

  36. How can you restrict that an attribute's value meets certain conditions when setting it?

    Implement validation logic inside the setter method. For example, raise an exception if the value is too long or negative.

  37. What is abstraction in OOP?

    The principle of hiding complex implementation details and exposing only essential features to the user.

  38. How can you hide a method from external access in Python?

    Prefix the method name with double underscore (e.g., def __connect(self):). This triggers name mangling, making it less accessible from outside the class.

  39. What is polymorphism in OOP?

    The ability of a single entity (function, method, operator) to work with different types of objects in different ways.

  40. How does inheritance enable polymorphism?

    A method defined in a parent class can be used by all child classes, and each child can override it to provide specific behavior. The same method call can produce different results depending on the object type.

Card 1 of 40
Card 1 of 40

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent