CHAPTER 7 - PYTHON METHODS & CLASSES



AD

7.1 Python Variables and Methods

Variables are instances of classes such as integers, strings, and lists. Variables are attributes. Methods act on or do something with the variables. For example, methods such as functions do something with variables. Functions are methods. A variable has many methods that can act on it.

#A variable is an instance of its class, such as integer
mynumber = 3
print(type(mynumber))


#Output class 'int'


#Attributes are variables, and methods are functions attached to the variable.
#A string has many built in methods we can use.
#Methods work like functions.
#A method is separated from the variable by a period.


#The replace() method replaces part of a string with another part.
#The original string is unchanged.

mystring = 'Steve Johnson'
newstring = (mystring.replace('Steve','John'))
print ("mystring = ", mystring)
print("newstring= ", newstring)


#Output
# mystring = Steve Johnson
# newstring= John Johnson


7.2 Creating a Python Class

Strings, integers, and lists are classes of objects. We can create our own classes of objects. Just as variables in Python are lowercase by convention. The name of the class has the first letter capitalized. IMPORTANT: If you copy and paste to PyCharm be sure the indents on the lines are correct, or you will get error messages!

#Variables are instances of Integer, Strings, or Lists - OR CLASSES
#To create our own classes we use the keyword class.
# A class is like a blueprint for creating objects
#Objects get their variables and functions from classes


#Initializing a class
class Employee:
   def __init__ (self, name, email, job):
     self.name = name
     self.email = email
     self.job = job

There are only three terms that are new to creating a class: class, def __init__, self
You will see these three terms over and over, each time a class is created. The word class just labels Employee as the name of the class. def says we are starting a function. __init__ says we are constructing a class. self just refers to itself.

Please note that __init__ consists of two underscores before and two underscores after init.

Name, email, job are classes of variables. All we did is create three new types of variables.


7.3 Creating two objects using the Python class

Now we create two new objects, employee_1, and employee_2.

class Employee:
   def __init__(self, name, email, job):
      self.name = name
      self.email = email
      self.job = job

# Now we create two objects, employee_1, and employee_2
employee_1 = Employee("S. Johnson", "johnson@dot.com", "Manager")
employee_2 = Employee("K. Smith", "smith@dot.com", "Clerk")
print(employee_1.job)


#Output Manager


7.4 Creating a method in the class

We can also define a new method in a class.


class Employee:
   def __init__(self, name, email, job):
     self.name = name
     self.email = email
     self.job = job

   def get_info(self):    # Define a method
     return '{} {} {}'.format(self.name, self.email, self.job)

# Now we create two objects, employee_1, and employee_2
employee_1 = Employee("S. Johnson", "johnson@dot.com", "Manager")
employee_2 = Employee("K. Smith", "smith@dot.com", "Clerk")
print(employee_1.job)
# Print the name, email, job of an instance of class method
print(employee_1.get_info())


#Output
# Manager
# S. Johnson johnson@dot.com Manager


7.5 We can also update an employee's info

class Employee:
  def __init__ (self, name, email, job):
    self.name = name
    self.email = email
    self.job = job
  def get_info(self): #Define a method
    return '{} {} {}'.format(self.name, self.email, self.job)
#Now we create two objects, employee_1, and employee_2
employee_1 = Employee("S. Johnson", "johnson@dot.com", "Manager")
employee_2 = Employee("K. Smith", "smith@dot.com", "Clerk")
print(employee_1.job)
#Update employee_1's job to Lead
employee_1.job = "Lead"
print(employee_1.job)
#There are also class methods or actions
#Print the name, email, job of an instance
print(employee_1.get_info())


#Output
# Manager
# Lead
# S. Johnson johnson@dot.com Lead


7.6 Adding an Employee class variable

A class variable applies to all objects created in the class. In the code below, we create the class variable location. Later in the program we print the location for each employee.

#Variables are instances of Integer, Strings, or Lists - OR CLASSES
#To create our own classes we use the keyword class.
# A class is like a blueprint for creating objects
#Objects get their variables and functions from classes
#Initializing a class
class Employee:
  location = "Fresno, CA" #Class variable
  def __init__ (self, name, email, job):
    self.name = name
    self.email = email
    self.job = job
  def get_info(self): #Define a method
    return '{} {} {}'.format(self.name, self.email, self.job)

#Now we create two objects, employee_1, and employee_2
employee_1 = Employee("S. Johnson", "johnson@dot.com", "Manager")
employee_2 = Employee("K. Smith", "smith@dot.com", "Clerk")
print(employee_1.job)
#Update employee_1's job to Lead
employee_1.job = "Lead"
print(employee_1.job)
#Now we print the Class variable location
print(employee_1.location)
print(employee_2.location)
#There are also class methods or actions
#Print the name, email, job of an instance
print(employee_1.get_info())


#Output
# Manager
# Lead
# Fresno, CA
# Fresno, CA
# S. Johnson johnson@dot.com Lead

The development of this Employee class is complete.

theSurfDragon.com


Win-Python Navigation

Table of Contents
Ch1-Starting Out
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Variable Scope
Ch6-Bubble Sort
Ch7-Intro to OOP
Ch8-Inheritance
Ch9-Plotting
Ch10-Files
Ch11-Print Format
Ch12-Dict-Zip-Comp
Ch13-Slice Arrays