Methods, Classes, & Inheritances Flashcards
Creating a Class
(General Syntax)
PEP 8: use camel casing for class names
class NameOfClass( ):
syntax below: creates an object instance
param1/param2 in the () not essential to create a parameter
def __init__ ( self, param1, param2) :
self. param1 = param1
self. param2 = param2 # assigning the parameter to an attribute of the class instance
def some_method ( self ):
perform some action
print ( self.param1 )
Assigning a Variable/Instance to a Class
(recall the 3 steps)
( 1 ) Define the Class
class DogClass:
init special method called upon to create an instance
self connects the method to the instance of the class by
refering to itself
def __init__ ( self, breed ) :
self.breed = breed # breed is the parameter
( 2 ) Assign an Instance
because there is a breed parameter, must be added to the dog instance when it is defined
my_dog_instance = DogClass( breed = ‘Labrador’ )
( 3 ) Verify the Instance is Assigned to the Class
type(class_instance)
understanding the componenets of defining a class
__init__ method
the self parameter
how to assign a parameter/attribute to a class ?
- __init__ is a constructor for a class, called automatically when an instance is called, allows attributes to be added to object instances
- self represents the instance of the class object
- self.param1 = param1 or self.attribute_name = attribute_name
assigning attributes at the class level
instead of the instance level
class DogClass( ):
class object attributes
same for any instance of a class
species = ‘mammal’
def __init__ (self, breed, name): # instance level attributes
self. breed = breed
self. name = name
methods
(def, how to call, function to find information about a method)
- a function that is avaiable to a given object because of the object type; a function that belongs to an object
- .method_name( )
- help( object.method)
calling an ( 1 ) attribute ( 2 ) method
- variable_name.attribtue
- variable_name.method( )
adding additional arguments to a method that are not attributes
(user input parameter)
class DogClass:
def __init__ ( self, name ):
self.name = name
def bark ( self, number ):
do not use self. when specify a user input parameter
print ( my name: { }, its age: { }”.format(self.name, number))
my_dog = DogClass(name = ‘billy’)
user has to provide the input value for any parameter not identified in the __init__ statement
my_dog.bark( 10 )
inheritance definition
froming new classes from classes that have already been defined
( 1 )
how to inherit from another class
( 2 )
how to overight methods from the parent class in the child class
you must call the original class in the inheriting class
Example:
class Animal ( ):
def __init__ ( self ) :
print ( “ Animal Created “ )
def who_am_i ( self ) :
print ( “ I am an Animal “ )
( 1 ) inherits by calling the original class
( 1 ) the methods from Animal are now callable in Dog
class Dog ( Animal ) :
def __init__ ( self ) :
Animal.__init__ ( self )
print ( “ Dog Created “ )
( 2 ) overwrite method by creating a new method with the
same name
def who_am_i ( self ) :
print ( “ I am a Dog “ )
polymorphism
( 3 versions )
( 1 )
functions with the same name that can be used on different data type and provides different outputs. Ex.:
len( “some text” ) : outputs the length of a string
len( [“jen”, “bev”] ) : outputs num. of items in a list
len( { “Name” : “Jon” , “City” : “DC” } ): outputs number of keys
( 2 )
different class objects that have methods with the same name but produce different outputs. Ex.:
class Cat:
def __init__(self, name) :
self.name = name
def make_sound(self) :
print(“Meow”) # method has same name diff. output
class Dog:
def __init__(self, name) :
self.name = name
def make_sound(self) :
print(“Dog”) # method has same name diff. output
( 3 )
inheritance were the child class overwrites a method with the same name Method Overwritting (see inheritance & overwritting methods note card)
inheritance
definition & 2 benefits
forming a new class using classes that have already been defined
reuse code and reduce the complexity of code