Python OOP Course
- 40Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is a class in Python?
A blueprint for creating custom data types. It defines attributes and methods that its instances (objects) will have.
Or swipe the card
40 cards
What is a class in Python?
A blueprint for creating custom data types. It defines attributes and methods that its instances (objects) will have.
How do you create an instance of a class in Python?
By calling the class name like a function:
item1 = Item()How do you assign attributes to an instance?
Using dot notation after the instance:
item1.name = "Phone"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.What is the
selfparameter 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.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.
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.
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 withself.attribute = parameter.What are instance attributes?
Attributes that are unique to each instance, typically assigned in
__init__usingself.attribute = value.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.
How do you define a class attribute?
By assigning a value inside the class body, outside any method. Example:
pay_rate = 0.8inside the class Item.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.
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.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.Why should you use
self.pay_rateinstead ofItem.pay_ratein a method that applies a discount?To allow per-instance pay rates to take effect.
self.pay_ratelooks for an instance attribute first; if absent, it falls back to the class attribute.What is the difference between instance methods and class methods?
Instance methods take
selfas first parameter and operate on instance data. Class methods takeclsas first parameter and operate on class-level data.How do you define a class method?
Use the
@classmethoddecorator above a method definition. The first parameter must becls(by convention).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.
What is a static method?
A method that does not receive any automatic first argument (neither
selfnorcls). It behaves like a regular function but belongs to the class's namespace.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.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.
What does the
@classmethoddecorator do?It converts a method into a class method, meaning the first argument passed automatically is the class itself (conventionally named
cls).What does the
@staticmethoddecorator do?It converts a method into a static method, removing the automatic passing of
selforcls. It behaves like a regular function inside the class.What is inheritance in OOP?
A mechanism where a child class derives properties and behaviors from a parent class, allowing code reuse.
How do you create a child class that inherits from a parent class?
class ChildClass(ParentClass):inside the class definition. Example:class Phone(Item):.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.What happens if a child class defines its own
__init__without callingsuper().__init__()?The parent's
__init__is not called, so parent attributes are not initialized unless explicitly assigned in the child's__init__.What warning does Python give if a child class defines
__init__without callingsuper().__init__()?A warning like 'Call to init of super class is missed'. It's a hint that you should call the parent's constructor.
How can you make the
__repr__method show the child class name instead of hardcodingItem?Use
self.__class__.__name__to dynamically get the class name. Example:return f"{self.__class__.__name__}('{self.name}', {self.price}, {self.quantity})".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.
How do you create a read-only attribute in Python?
Use the
@propertydecorator on a method that returns the attribute's value. This allows access but prevents assignment (unless a setter is defined).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.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.What is name mangling in Python?
The process where an attribute with double underscore prefix (e.g.,
__name) is transformed to_ClassName__nameto avoid accidental overriding in subclasses.What is the difference between single underscore
_nameand 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).
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.
What is abstraction in OOP?
The principle of hiding complex implementation details and exposing only essential features to the user.
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.What is polymorphism in OOP?
The ability of a single entity (function, method, operator) to work with different types of objects in different ways.
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.

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