Attributes
Data that belong to an object
Feature of a class
e.g. type, make and model of class Vehicle
Methods
functions that are associated with a class, and perform operations on the data associated with an instantiation of the class
class
Blueprint for creating object
Object
an instance of a class (real thing created from the blueprint)
What happens when you define a class
You need to define a set of Attributes
Object
An instance (example) of a class
What is the shape attribute of a NumPy array?
Describes the number of entries in the array along each dimension
How can you tell the difference between an attribute and a method?
Attribute accessed without parentheses and methods use them since they are functions
Why do attributes not use parentheses?
Because accessing an attribute does not perform computation -just retrieves stored data
Can object methods take other objects as arguments?
Yes
Where can you find a list of class attributes and methods
In class documentation
Why might you create your own class?
When no existing class suits your problem
What does a class represent?
A blueprint for creating objects with attributes and methods
what is the purpose of the __init__ method?
It initialises an object when it is created and sets up its attributes
what does self refer to in a class?
The object itself
What typically goes inside the __init__ method?
Assignment that define the object’s attributes
What is the role of class methods?
To perform operations using the object’s attributes
What are magic methods?
Special methods with double underscore that define how operators behave
Why are magic methods usually not called directly?
Because python calls them automatically when operators are used
What happens if you se an operator that a class does not define
Python raises an error
What does the __str__ method control?
how an object is displayed when printed
How can you redefine how mathematical operators work for a class?
By implementing the corresponding magic methods.
What is the benefit of defining custom operator behaviour?
It allows objects to work naturally with Python syntax.
Why do built-in sorting functions work with many object types?
They rely only on comparison operators, not object details.