Working with Object Oriented Programming
For my 3rd blog post I'll be sharing how I completed a checkpoint assignment for my Data Science course at the FlatIron School. One of the central topics of our Phase 3 course material is Object Oriented Programming. This entails creating classes, Instantiating Objects, and then updating an instance attribute.
Initial Information Given
The initial information given were three different dictionaries containing information about Pokemon characters. The information of Pokemon characters from these dictionaries are name, weight, height, base experience, and types. One of the main tasks of the project was to use the dictionaries to create Pokemon objects. These are the dictionaries provided:
bulbasaur_data = {"name": 'bulbasaur', "weight": 69, "height": 7, "base_experience": 64, "types": ["grass", "poison"]}
charmander_data = {"name": 'charmander', "weight": 85, "height": 6, "base_experience": 62, "types": ["fire"]}
squirtle_data = {"name": 'squirtle', "weight": 90, "height": 5, "base_experience": 63, "types": ["water"]}
Creating a Class
The first step for this checkpoint was to create a class with a function called init that can take items from the dictionaries provided and output the details of the Pokemon character. This includes the name, weight, height, base experience, and types. For this specific task the name, weight, and height were the primary columns of interest. The outcome of creating this class is below:
class Pokemon:
def init(self,data,name,weight,height):
self.name = name
self.weight = weight
self.height = height
The idea here is that the function from the class Pokemon is going to take the name, weight, and height from the dictionaries of Pokemon characters and print out the corresponding characteristics.
Instantiating Objects
Now that we've created a class for the Pokemon dictionaries and have a function within that class, we can add the Pokemon dictionaries to the Pokemon class to generate those corresponding characteristics we're looking for. The first dictionary with a Pokemon character is bulbasaur so we can use that as a demonstration of how the dictionary fits into the Pokemon character function.
bulbasaur = Pokemon(bulbasaur_data,bulbasaur['name'],bulbasaur ['weight'], bulbasaur_data['height'])
This is a Pokemon Object that can then be used to generate the corresponding characteristics. The breakdown of the steps to generate these results is below:
- Create dictionary of Pokemon characters
- Create a class and function for Pokemon characters to generate specific items of dictionary that describe the character
- Create an Object where class and function is aligned with information from the dictionary
- Create a function that prints the objects with corresponding information about the Pokemon character
Here is the function that takes an object and prints it's information:
def print_pokeinfo(pkmn):
print('Name:' + pkmn.name)
print('Weight:'+ str(pkmn.weight))
print('Height:'+ str(pkmn.height))
print('\n')
print_pokeinfo(bulbasaur)
Prints:
Name: Bulbasaur
Weight: 69
Height: 7
This function takes in a dictionary and prints the requested items of that dictionary.
Update an Instance Attribute
class weight:
def init(self,data,name,weight,height):
self.name = name
self.weight = weight
self.weight += 5
self.height = height
charmander = weight(charmander_data,charmander_data['name'],
charmander_data['weight'],charmander_data['height'])
print_pokeinfo(charmander)
Prints:
Name: Charmander
Weight: 90
Height: 6
As you can see from the results, the function we created for the weight class the weight increased from 85 to 90 because of the 5 we included as a sum in the function.
Top comments (0)