Classes, Object-Oriented Programming and Inheritance Flashcards
What is an Object?
An object is an instance of a type - Everything in python is an option!
- each object has a type
- an internal data representation
- an interface for interacting with the object
i. e. 1234 is an instance of a type
What are the advantages of Object Oriented Programming?
- Bundle data into packages together with procedures that work on them through well defined interfaces
- divide and conquer development
- -> implement and test behavior of each class separately
- classes make it easy to reuse code
What do you have to do to create a class?
- Define the class name (=type i.e. student)
- Define class attributes (what is the object?)
- Define methods (How to use the objects?)
What do you have to do to use the class?
- Create a new instance of a class (class objects)
2. Do operations on these instances
What do you use the self for?
To refer to some instance while defining the class i.e. class Coordinate (object): (self.x - self.y) ** 2
What is the main difference between a class and one of its objects?
A class defines data and methods across all instances vs. An instance has the structure of the class
What are data attributes within a class?
They represent your object with data
i.e. for an animal: age, name
for a coordinate: x and y values
What are procedural attributes within a class?
Methods (Behaviour/operations)
- -> How you can interact with an object of the class, what it does
i. e. for a coordinate: find distance between two
What are getter and setter methods used for?
Should be used outside the class to access data attributes
What is the dot notation used for with objects?
the dot is used to access attributes(data and methods)
but use primarily for methods and use getters and setters for data attributes