Methods, Classes, & Inheritances Flashcards

1
Q

Creating a Class

(General Syntax)

A

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 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assigning a Variable/Instance to a Class

(recall the 3 steps)

A

( 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

understanding the componenets of defining a class

__init__ method

the self parameter

how to assign a parameter/attribute to a class ?

A
  • __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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

assigning attributes at the class level

instead of the instance level

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

methods

(def, how to call, function to find information about a method)

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

calling an ( 1 ) attribute ( 2 ) method

A
  1. variable_name.attribtue
  2. variable_name.method( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

adding additional arguments to a method that are not attributes

(user input parameter)

A

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 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

inheritance definition

A

froming new classes from classes that have already been defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

( 1 )

how to inherit from another class

( 2 )

how to overight methods from the parent class in the child class

A

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 “ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

polymorphism

( 3 versions )

A

( 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

inheritance

definition & 2 benefits

A

forming a new class using classes that have already been defined

reuse code and reduce the complexity of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly