OBJECT ORIENTED PROGRAMMING PYTHON
TH.S HUNG NGUYEN MANH
OUTLINE
Introduction OOP Class variable
1 2 3
Class basic
I. INTRODUCTION OBJECT ORIENTED
Introduction OOP
Four tenants of OOP
Class Diagram Example: Order System
I.1 OBJECT ORIENTED WITH PYTHON
Object-Oriented Programming (OOP) is the
programming approach that models real-world
objects and their interactions.
OOP focuses on the thinking process,
implementation of what the developer wants to
manipulate rather than the logic required to
manipulate them
I.2 FOUR TENANTS OF OOP
Encapsulation allows you to bundle data (attributes) and behaviors (methods) within a class to create a cohesive
unit. By defining methods to control access to attributes and its modification, encapsulation helps maintain data
integrity and promotes modular, secure code.
Inheritance enables the creation of hierarchical relationships between classes, allowing a subclass to inherit
attributes and methods from a parent class. This promotes code reuse and reduces duplication.
Abstraction focuses on hiding implementation details and exposing only the essential functionality of an object. By
enforcing a consistent interface, abstraction simplifies interactions with objects, allowing developers to focus on
what an object does rather than how it achieves its functionality.
Polymorphism allows you to treat objects of different types as instances of the same base type, as long as they
implement a common interface or behavior.
I.3 CLASS DIAGRAM EXAMPLE: ORDER SYSTEM
II. OBJECTS IN OOP
Object is an instance of a class in a particular moment in runtime that can have its own state
and data values.
Create a class diagram to describe the structure of a system and then create a set of object
diagrams as test cases to verify the accuracy and completeness of the class diagram.
Before you create a class diagram, you might create an object diagram to discover facts
about specific model elements and their links, or to illustrate specific examples of the
classifiers that are required.
II. OBJECTS EXAMPLE
II.1 CLASS TO OBJECT DIAGRAM EXAMPLE - ORDER SYSTEM
II.2 CLASS DIAGRAM VS. OBJECT DIAGRAM
An object diagram is a UML structural diagram that shows the instances of the classifiers in
models.
Object diagrams use notation that is similar to that used in class diagrams.
Class diagrams show the actual classifiers and their relationships in a system
Object diagrams show specific instances of those classifiers and the links between those
instances at a point in time.
II.2 OBJECT DIAGRAM EXAMPLE - WRITER
III. OBJECT IN PYTHON
Everything in Python is an object. An object has three attributes:
➢ Identity
➢ Type
➢ value.
IV. CLASS
The class contains two types of information about the object:
➢ Structural information:
➢ Behavioral Information:
IV.1 WRITE A PYTHON CLASS
Syntax:
Example: Student class
IV.2 INIT METHOD
The initialization method init(self, args)
The initialization method (Constructor) is a special method of a class, and it is always
named init.
The first parameter of the constructor is always self (a keyword referring to the class
itself).
The constructor is used to create an object. The constructor assigns values from the
parameters to the attributes of the object to be created.
You can define at most one initialization method (constructor) in a class.
If a class does not define a constructor, Python by default assumes that it inherits
from the parent class's constructor. :
IV.2 INIT METHOD
Example:
IV.3 METHOD - __STR__(SELF)
IV.3 METHOD - __STR__(SELF)
Example:
IV.3 METHOD - __STR__(SELF)
Example:
V. CREATE OBJECT FROM CLASS
Syntax:
➢ nameOfObject = nameOfClass([args])
Example
➢ student = Student('Jake', 24, 85, 90, 77)
V.1 OBJECT METHODS
Objects can also contain methods. Methods in objects are functions that belong to the object.
Example
V.2 THE SELF PARAMETER
The self parameter is a reference to the current instance of the class, and is used to access
variables that belong to the class.
It does not have to be named self, you can call it whatever you like, but it has to be the first
parameter of any function in the class:
Example
5.3 ACCESSING ATTRIBUTES AND METHODS
You can access the attributes and methods of an object by using dot notation with
the dot operator.
The following snippet of code shows the required syntax:
5.3 ACCESSING ATTRIBUTES AND METHODS
Example:
5.4 NAMING CONVENTIONS IN PYTHON CLASSES
Most Python programmers follow the snake_case naming convention, which
involves using underscores (_) to separate multiple words.
This is used for functions and methods.
However, the recommended naming convention for Python classes is the
PascalCase, where each word is capitalized.
5.4 NAMING CONVENTIONS IN PYTHON CLASSES
Example:
6. CLASS OR STATIC VARIABLES
A static variable is a variable that is shared among all instances of a class, rather than being
unique to each instance.
It is also sometimes referred to as a class variable because it belongs to the class itself
rather than any particular instance of the class.
Static variables are defined inside the class definition, but outside of any method definitions.
They are typically initialized with a value, just like an instance variable, but they can be
accessed and modified through the class itself, rather than through an instance.
6. CLASS OR STATIC VARIABLES
Example
6.1 FEATURES OF STATIC VARIABLES
• Memory Efficiency: Static variables are allocated memory once when the object for the class is
created for the first time.
• Class Scope: Static variables are created outside methods but inside the class.
• Access Through Class: Static variables can be accessed through the class but not directly
through an instance.
• Consistent Behavior: The behavior of static variables doesn’t change for every object.
6.1 FEATURES OF STATIC VARIABLES
• Example
6.2 DIFFERENCES BETWEEN CLASS VARIABLES IN PYTHON AND STATIC
VARIABLES IN JAVA/C++
❑ Java/C++ Behavior: When you modify a static variable in Java or C++, the change is reflected
across all instances of the class, and they all remain synchronized with the static variable’s
value.
❑ Python Behavior: In Python, if you modify a class variable through an instance, a new
instance variable is created. This separates the modified value from the original class
variable, which remains unchanged for other instances.
6.3 INSTANCE VARIABLES VS CLASS VARIABLES IN PYTHON
❑ Instance Variables: Variables that are unique to each object. They are created inside
methods (typically __init__()).
❑ Class Variables (Static Variables): Variables shared among all instances of a class. They
are defined within the class, outside any methods.
6.4 BEST PRACTICES FOR USING CLASS VARIABLES
❑ Use Class Variables for Shared Data: If you want all instances to share a particular value or
state, use class variables.
❑ Modify Class Variables via Class Name: Always modify class variables using the class
name ([Link]) to avoid unintentionally creating an instance-level copy.
❑ Avoid Modifying Class Variables from Instances: Modifying class variables through
instances can lead to confusion, as it creates instance-level variables.
❑ Be Careful in Multithreaded Environments: In multithreaded programs, unsynchronized
class variables can cause race conditions.
6.5 ADVANTAGES
❑ Memory efficiency: Since static variables are shared among all instances of a class, they can save
memory by avoiding the need to create multiple copies of the same data.
❑ Shared state: Static variables can provide a way to maintain shared state across all instances of a
class, allowing all instances to access and modify the same data.
❑ Easy to access: Static variables can be accessed using the class name itself, without needing an
instance of the class. This can make it more convenient to access and modify the data stored in a
static variable.
❑ Initialization: Static variables can be initialized when the class is defined, making it easy to ensure
that the variable has a valid starting value.
❑ Readability: Static variables can improve the readability of the code, as they clearly indicate that
the data stored in the variable is shared among all instances of the class.
6.6 DISADVANTAGES
❑ Inflexibility: Static variables can be inflexible, as their values are shared across all instances
of the class, making it difficult to have different values for different instances.
❑ Hidden dependencies: Static variables can create hidden dependencies between different
parts of the code, making it difficult to understand and modify the code.
❑ Thread safety: Static variables can be problematic in a multithreaded environment, as they
can introduce race conditions and synchronization issues if not properly synchronized.
❑ Namespace pollution: Static variables can add to the namespace of the class, potentially
causing name conflicts and making it harder to maintain the code.
❑ Testing: Static variables can make it more difficult to write effective unit tests, as the state of
the static variable may affect the behavior of the class and its methods.