1/34
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is a class in python?
A template (blueprint) for creating objects it defines attributes (data) and methods (functions
one class can have many objects
What is an object?
an object is an instance of a class
How are classes and objects related?
an object is an instance of a class
one class can be used to create multiple objects (i
What othwr built in classe sin python have been covered?
int string list etc
are all classes
What does x = 12 what class is x an instance of?
x is part of the int class
if name = Helen, what class is name an instance of?
the string class (str)
What is a class attribute (class variable)
A variable defined inside a class that is shared by all instances of that class.
In the following class, what are x, y, and z?
x = 10
y = 6
z = 9
Class attributes
What does pos1 = MyPosition() do?
it adds another object instance of the class (My position)
Do class attributes have the same value for every object?
Yes unless directly specified for a specific instance
What prints when you run this?
pos1 = MyPosition()
print(pos1.x)
prints the value of the class attribute in this case x
Why use Object oriented prgramming (OOP)?
instead of one big main file the data is seperated into different classes
each class defines its own variables and methods (functions)
you work with objects and data rather than functions or logic
What things define object oriented programming?
CLasses the building block
Objects instance of a class
methods functions that objects preform
attributes ( the state of an object aka the data)
What are the 4 benefits of OOP?
code reusability
improved moduality and organization
easier to maintain and update
better flexibility and scalability for adding new features
What is abstraction?
The inner workings of the class (and all its details) is hidden from outside the class
the process of hiding complex implementation details and only showing the essential features of an object
focus on what the object does rather than how it does it
boundary between internal implementation and external interface allowing the internal details to be changed without affecting how the object is used
What is encapsulation?
Variable sinside the class should be protected from the outside manipulation
the outsider should not be allowed to see or change the details inside the class such as its variables
python does NOT force encapuslation
should always call a method to access or change a variable
What is the main difference between abstraction and encapsulaion?
abstraction hides the implementation details (car is a car, not all its components)
encapsulation protects and has control access to the data (like a shield) to prevent unauthorized access
What is a contructor in python?
a special method called (__init__)?that runs automatically when an object is created
What is the main purpose of the constructor (__init__)?
to initialize and set the instances attributes so each object starts at a consistent state
What does self refer to inside __init__?
The specific instance (object) being created.
What is the difference between a class attribute and an instance attribute?
Class attribute → shared by all objects
Instance attribute → unique to each object, defined inside __init__
What happens if a class has no __init__ method?
Python uses a default constructor that doesn’t initialize attributes.
What does "instantiating a class" mean?
Creating an object by calling the class like a function → e.g., MyPosition(10, 15, 20).
What is a module in Python?
A file containing Python code (variables, functions, classes) that can be imported into other programs.
What does the shapes module contain?
Three classes: Circle, Triangle, and Square.
What is the constructor for the Circle class?
def init(self, r):
self.radius = r
A method that initializes the radius of the circle.
What is the instance variable in the Circle class?
self.radius
What is the constructor for the Triangle class?
def init(self, b, h):
self.base = b
self.height = h
What does the Square class currently contain
class Square:
pass
none
What keyword is used to create an empty class?
pass
How do you import all classes from the shapes module?
from shapes import Circle, Triangle, Square
Why does c.calc_area() work?
Because c is an object with a method defined inside its class.