Introduction to OOP
Completion requirements
Let us review our first object-oriented Python program to clarify some things.
Class definition
Before we can create objects, there must be defined the object template - the so-called class:
1 2 3 4 5 6 7 8 9 10 |
class User: # class definition header -> class classname: def __init__(self, name, password): # constructor = special method, which is used for creating object self.__name = name # private attributes "name" and "password" are initialized self.__password = password # double underscore "__" means private def changePassword(self, password): # method changePassword sets new value to attribute "password" self.__password = password def getPassword(self): # Getter method only get back value to attribute (here from "password") return self.__password |
The Unified Modeling Language UML (a collection of diagrams) is often used to visualize object-oriented structures:

UML class diagram
Creation of objects
No object has been created yet. This is done with
11 |
user1 = User("Miller","Miller's PWD") # creates object "user1" (constructor is invoked implicitly) |
The constructor defined above is used here. Values (here strings) are passed as parameters and assigned to the properties (attributes). The following UML object diagram shows these assigned values:

UML object diagram
Use of objects
Methods are called as follows:
12 13 | user1.changePassword("Miller's new PWD"); # Method "changePassword" of object "user1" is called (dot separator) print(user1.getPassword()) # Method "getPassword" returns the value of attribute "__password" |
Note: For a call from outside the object, the methods must be declared as public (no "__" prefix).Output
Miller's new PWD
Last modified: Monday, 13 March 2023, 8:33 AM