User Guide

145
To access to the fields and methods that is inside an object, use
the dot syntax.
Alice.firstname
Alice.lastname
You can add methods to the class by indenting them. To refer to
fields inside the object from methods, use the keyword self.
class AddressBookEntry:
firstname = ""
lastname = ""
telephone = ""
def printName(self):
print(self.firstname + " " + self.lastname)
To call the printName() method from other parts of your code,
specify the variable name, then a dot, and then the method
name. For example:
Alice.printName()
__init__ is a special method that Python runs when an instance of
the class is created. You can write your own and use this to
ensure that the correct arguments are passed into the code
when you try to create an instance of the class. For example:
class AddressBookEntry:
firstname = ""
lastname = ""
telephone = ""
def __init__ (self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
Mark = AddressBookEntry(firstname="Mark",lastname="White")
Inheriting Fields and Methods
Inheritance is where objects that are similar share parts of their
structure. Using the address book example again, you could
have a separate object for business contacts. Since this object