2. Classes
• A class describes a set of objects with the same behavior.
• String class
• Constants:
• Ascii_letters – all ascii_lowercase and ascii_uppercase letters
• Ascii_lowercase - 'abcdefghijklmnopqrstuvwxyz’
• Ascii_uppercase - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ’
• Digits - '0123456789’
myString = 'Hello Cruel_World!'
for i in myString:
#myString[i] = random.choice(string.ascii_letters)
print(random.choice(string.ascii_letters))
4/7/2019CS151SP19
3. Class Methods
• The set of all methods provided by a class, together with a description of
their behavior, is called the public interface of the class.
• You can drive a car by operating the steering wheel and pedals, without knowing how
the engine works. Similarly, you use an object through its methods. The
implementation is hidden.
• String methods
• isapha() – returns bool if all characters are letters
• isdigit() – returns bool if characters are digits (numbers)
• islower() – returns bool if all characters are lowercase
import string
myString = 'Hello Cruel_World!'
for i in myString:
if i.isalpha:
print( i, ' is a Letter!')
else:
print(i, ' is not a letter!')
4/7/2019CS151SP19
4. Class Constructors
• A constructor defines and initializes the instance variables of an
object. The constructor is automatically called whenever an object is
created.
• The constructor is responsible for defining and initializing all of the
instance variables that are to be contained in the object. After the
constructor completes its work, a reference to the newly created and
initialized object is returned. The reference is saved in a variable so
we can later call methods on the object.
4/7/2019CS151SP19
5. Constructor Example
Class Definition
class Canine:
''' A simple dog class implementation '''
name = ''
breed = []
def __init__(self, name):
self.name = name
self.breed = []
Example Code
mydog = Canine('fido’)
• This creates an object of the class
Canine and specifies the name fido
• Does it specify the breed?
• Do we need to?
4/7/2019CS151SP19
6. Methods
Class Definition
• Need method to get name and
breed!
def get_name(self):
print(self.name)
def get_breed(self):
print(self.breed)
Example
pet = myDog.get_name()
• Hey you didn’t set the breed
anywhere?
4/7/2019CS151SP19
7. Set Method
Class Definition
• Lets define set_breed()
def add_breed(self, breed):
self.breed.append(breed)
Code
myDog.add_breed(‘Poodle’)
myDog.add_breed(‘Labrador’)
So what does our class look like
now?
4/7/2019CS151SP19
8. Class Example
class Canine:
''' A simple dog class implementation '''
name = ''
breed = []
def __init__(self, name):
self.name = name
self.breed = []
def add_breed(self, breed):
self.breed.append(breed)
def get_breed(self):
print(self.breed)
def get_name(self):
print(self.name)
• Name = ‘fido’
• Breed = [‘Poodle’, ‘Labrador’]
• The class is complete but it does very
little.
• Can we add dog age?
• Where would we put it?
• What accessor methods do we need?
4/7/2019CS151SP19
10. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
4/7/2019CS151SP19
11. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
4/7/2019CS151SP19
12. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
4/7/2019CS151SP19
13. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
4/7/2019CS151SP19
14. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
4/7/2019CS151SP19
15. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
4/7/2019CS151SP19
16. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
4/7/2019CS151SP19
17. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
• How about you have to write a program to control inventory in a factory?
4/7/2019CS151SP19
18. Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
• How about you have to write a program to control inventory in a factory?
• Why would you use a class?
• Can’t all this be programmed using what we already know?
4/7/2019CS151SP19
19. Deconstructing the problem?
• Writing good programs requires you to analyze the problem.
• What is the problem?
• Can the problem be solved incrementally?
• Is it even solvable, if not can you estimate an answer with the data on hand?
• What is the data?
• Is it being collected or given to you?
• What is common about the data?
4/7/2019CS151SP19
20. Deconstructing the problem?
• Writing good programs requires you to analyze the problem.
• What is the problem?
• Can the problem be solved incrementally?
• Is it even solvable, if not can you estimate an answer with the data on hand?
• What is the data?
• Is it being collected or given to you?
• What is common about the data?
4/7/2019CS151SP19
21. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
4/7/2019CS151SP19
22. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
4/7/2019CS151SP19
23. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
4/7/2019CS151SP19
24. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
4/7/2019CS151SP19
25. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
4/7/2019CS151SP19
26. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
• We could reconfigure our class from Canine to Pet!
4/7/2019CS151SP19
27. Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
• We could reconfigure our class from Canine to Pet
• Adding a private data type to hold, cat or dog
4/7/2019CS151SP19
28. Pet Class
class Pet:
name = ‘’
breed = []
animal=‘’
def __init__(self, name, animal):
self.name = name
self.breed = []
self.animal=‘’
def add_breed(self, breed):
self.breed.append(breed)
def get_breed(self):
print(self.breed)
def get_animal(self):
print(self.animal)
4/7/2019CS151SP19