24 Questions
Where are class variables defined in a class block?
at the top of the class block without the self keyword
How are class methods defined?
without self as a parameter
How are class variables and methods referenced?
with the class name
What does the None keyword typically define?
that a variable has no value
What is the process called when a new class is created from another class?
inheritance
What is the original class called in inheritance?
base/parent/super class
What does a derived class automatically inherit from the base class?
all the object variables and methods
What is an advantage of using inheritance?
code can be reused without copying the base class details
What is the term used to describe a class that is inherited from by another class?
ancestor class
What is the relationship between class A and class B if class A is an ancestor of class B?
class B is a descendant of class A
What is a key feature of Python's inheritance mechanism?
multiple inheritance
How are superclasses separated in Python's multiple inheritance?
by commas
What is the term used to describe a subclass providing a specific implementation of a method inherited from its superclass?
method overriding
What is the purpose of method overriding in object-oriented programming?
to provide a specific implementation of a method inherited from a superclass
What is the purpose of the __init__(self)
method in a class?
To initialize the object variables when a new object is created
What is the significance of the self
keyword in object-oriented programming?
It refers to the current object instance
How do object variables differ from regular variables?
Object variables are prefaced with self and are used to define the data which needs to be stored for each object
What is the purpose of the __str__(self)
method?
To convert an object to a string and return a human-readable representation
What is class reuse in object-oriented programming?
The ability to use a class in different programs multiple times
What is the difference between object methods and functions?
Object methods are defined within a class and act on a particular object type, while functions are standalone operations
What is the role of encapsulation in object-oriented programming?
Everything for an object is defined/enclosed in a class
How do object methods differ from regular functions?
Object methods have self as the first parameter and are defined within a class
What is the purpose of a class in object-oriented programming?
To define a new data type and solve a problem
What is the significance of the class keyword in object-oriented programming?
It defines a new class with its heading ending with a colon and body indented
Study Notes
Object Orientation
- Object-oriented algorithm simulates activities of several explicit actors, each with specific steps to perform.
- Each actor is free to use any appropriate technique to accomplish its task.
- Combined effort of all these simulated actors results in the goal of the algorithm.
Object Orientation Principles
- Encapsulation: everything for an object is defined/enclosed in a class.
- Inheritance: new objects can be based on existing objects.
- Polymorphism: operations can act on more than one type.
Classes
- A class is a template/blueprint for a new data type.
- The class keyword defines a new class with its heading ending with a colon and body indented.
- A class contains object methods and object variables.
- Object methods define the actions or operations which can be applied to an object.
- Object variables define the data/properties which need to be stored for each object.
Class Example
- A class can be defined with a constructor, object methods, and object variables.
- Example:
class Person: ...
Object Methods
- Methods are operations which can be applied to an object.
- Methods for the class are defined in its body and conform to the same rules as functions.
- Object methods have self as the first parameter.
- There are standard methods using the double underscore naming convention, such as
__init__(self)
and__str__(self)
. - User-defined methods can also be defined.
self
- The self keyword refers to the current object instance.
- self needs to be used explicitly when specifying object methods and variables.
init(self)
- The
__init__(self)
method is called the constructor and is called when a new object is created. - The constructor is used to initialize the object variables.
- The constructor can use default parameters.
Object Variables
- Object variables, also called instance variables, are used to define the data which needs to be stored for each object.
- Object variables are prefaced with self.
- The constructor
__init__(self)
is used to initialize the object variables.
str(self)
- The
__str__(self)
method converts an object to a string and is called when an object is printed out. - The
__str__(self)
method typically returns the concatenation of the object variables.
Class Reuse
- Once a class is defined, it can be reused over and over again in different programs.
- A class should be saved in a filename, with only one copy.
- Class variables are defined at the top of the class block without the self keyword.
Class Variables and Methods
- Class methods are defined without self as a parameter.
- Class variables and methods are referenced with the class name.
None
- The None keyword typically defines that a variable has no value.
- A variable can be checked if it is None.
Inheritance
- Inheritance is the process by which a new class is created from another class.
- The original class is called the base/parent/super class.
- The new class is called a derived/child/sub class.
- A derived class automatically inherits all the object variables and methods of the base class.
Inheritance Example
- A derived class can inherit characteristics and behaviors from its base class.
- Example: Children often inherit certain characteristics from their parents.
Multiple Inheritance
- Python allows multiple inheritance, meaning that a base class can inherit from more than one super class.
- Super classes are separated by commas.
Method Overriding
- Subclasses can override methods inherited from their superclasses.
Learn about the principles of object-oriented programming, including encapsulation, inheritance, and polymorphism. Understand how objects interact to achieve a common goal.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free