- In the Person Example, look at the
Person user defined class
Person in person.py and the test file in
test.py.
- For when a class (for example Person) is defined in a script, the interpreter running the script generates
a constructor method with the same name as the name as the class.
- The constructor method Person creates a new Person object:
p = Person("Alice", "F", 11)
The constructor then calls the __init__ method to initialize the instance variables of the object.
- The programmer should never call the __init__ method
directly.
Bad: p.__init__("Alice", "F", 11)
The programmer should call the constructor, then let the constructor automatically call
__init__:
Good: p = Person("Alice", "F", 11)
- How do we know what to pass in to the contstructor?
Answer: This constructor has the input arguments ("Alice", "F", 11) of datatypes
(str, str, int). This is because the __init__ method
of the Person class has parameters of datatypes
(str, str, int).
- In the preceding bullet point, (str, str, int) is the signature of the
__init__ and Person methods.
The class constructor (in this case Person) always has the same signature as the
__init__ method.
- With the statement:
p = Person("Alice", "F", 11)
after creating the person object, the constructor Person passes the three arguments
"Alice", "F", 11 to the
__init__ method of the Person class and calls it like this:
self.__init__("Alice", "F", 11)
- If no __init__ method is present in a class, a blank
object is created with no instance variables. In this case the
Person constructor has the empty signature ( ).
- An instance method with leading and trailing double underscores is called a
dunder method.
- The only dunder methods we will discuss in this course are
__init__ and __str__.
- Dunder methods usually define standalone methods or operators with different names. For example:
Dunder Method | Corresponding Standalone Method or
Operator |
* __init__ | Constructor |
* __str__ | str |
__repr__ | repr |
__eq__ | == |
__lt__ | < |
__add__ | + |
__mul__ | * |
* Discussed in IT 211.
- For example, if the name of the class is Screen, a new
Screen object is created like this:
s = Screen( )
- For an object p of the Person class, when the standalone method
str is called like this:
s = str(p)
the __str__ method is used to determine how the object
p is converted to a string.
- Predict the output of test1.py from the
Person Example. Ans:
Alice;F;11
Alice F 11
12
- Predict the output of this script that uses the Person class:
from person import Person
a = Person("Kendall", "F", 14)
a.have_birthday( )
b = Person("Mario", "M", 12)
a.have_birthday( )
print(b.name, a.gender, b.age)
b.have_birthday( )
print(b)
print(a)
# Variable trace table # Variable trace table # Output:
for object a: for object b: Mario F 12
name gender age name gender age Mario;M;12
--------+--------+----- --------+------+----- Kendall;F;16
"Kendall" "F" 14 "Mario" "M" 12
15
16
- Predict the output of the script in the
test2.py script of the Pet Example.
- Design, implement, and test the following classes:
BankAccount
Card VendingMachine
- Look at the BankAccount, Card, and VendingMachine Examples. Identify the
following items in the classes and in the test1.py
scripts:
Class Name Instance Variable Instance Method
Public Member
Private Member Dunder Method
Constructor Local Variable
Standalone Method Parameter Argument
Constant
- Predict the output of the test1.py and
test2.py scripts for the BankAccount, Card, and VendingMachine Examples.