Quantcast
Channel: sunny
Viewing all articles
Browse latest Browse all 44

[Python] codecademy-20140321

$
0
0

+ more on __init__() and self

# Class definition
class Animal(object):
    """Makes cute animals."""
    # For initializing our instance objects
    def __init__(self, name, age, is_hungry):
        self.name = name
        self.age = age
        # add third argument
        self.is_hungry = is_hungry

# Note that self is only used in the __init__()
# function definition; we don't need to pass it
# to our instance objects.

zebra = Animal("Jeffrey", 2, True)
giraffe = Animal("Bruce", 1, False)
panda = Animal("Chad", 7, True)

print zebra.name, zebra.age, zebra.is_hungry
print giraffe.name, giraffe.age, giraffe.is_hungry
print panda.name, panda.age, panda.is_hungry

+ A Methodical Approach

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age
        
hippo = Animal("Bagheera", 25)
hippo.description()

+ They’re Multiplying!

class Animal(object):
    """Makes cute animals."""
    is_alive = True # member variable
    
    # add member variable
    health = "good" # member variable

    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Add your method here!
    def description(self):
        print self.name
        print self.age
        
hippo = Animal("hippo", 25)  
sloth = Animal("sloth", 12)
ocelot = Animal("ocelot", 3)

print hippo.health
print sloth.health
print ocelot.health

+ It’s not all animals and fruits

class ShoppingCart(object):
    """Creates shopping cart objects
    for users of our fine website."""
    items_in_cart = {}
    def __init__(self, customer_name):
        self.customer_name = customer_name

    def add_item(self, product, price):
        """Add product to the cart."""
        if not product in self.items_in_cart:
            self.items_in_cart[product] = price
            print product + " added."
        else:
            print product + " is already in the cart."

    def remove_item(self, product):
        """Remove product from the cart."""
        if product in self.items_in_cart:
            del self.items_in_cart[product]
            print product + " removed."
        else:
            print product + " is not in the cart."

# create instance
my_cart = ShoppingCart("jskim")
# use the add_item method
my_cart.add_item("orange", 1000)

+ Inheritance Syntax

class Shape(object):
    """Makes shapes!"""
    def __init__(self, number_of_sides):
        self.number_of_sides = number_of_sides

# Add your Triangle class below!
class Triangle(Shape):
    def __init__(self, side1, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

+ Override

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
    
    # override method
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
        
emp = Employee("jskim")
part_emp = PartTimeEmployee("john")

print emp.calculate_wage(10)
print part_emp.calculate_wage(10)

+ This Looks Like a Job For…

class Employee(object):
    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 20.00

# Add your code below!
class PartTimeEmployee(Employee):
    
    # override method
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
        
    # built-in super call
    def full_time_wage(self, hours):
        # return super(PartTimeEmployee, self).method(args)
        return super(PartTimeEmployee, self).calculate_wage(hours)
        
emp = Employee("jskim")
milton = PartTimeEmployee("john")

print emp.calculate_wage(10)
print milton.calculate_wage(10)
print milton.full_time_wage(10)

+ Class Basics

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3

+ Class It Up
add a member variable and a method to our class.

class Triangle(object):
    # add member variable
    number_of_sides = 3
    
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        
    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False

+ Instantiate an Object

class Triangle(object):
    # add member variable
    number_of_sides = 3

    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        
    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False
            
my_triangle = Triangle(90,30,60)
print my_triangle.number_of_sides # 3
print my_triangle.check_angles() # True

+ Inheritance

class Triangle(object):
    # add member variable
    number_of_sides = 3

    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        
    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False

class Equilateral(Triangle):
    angle = 60
    
    def __init__(self):
        self.angle1 = self.angle
        self.angle2 = self.angle
        self.angle3 = self.angle
            
my_triangle = Triangle(90,30,60)
print my_triangle.number_of_sides # 3
print my_triangle.check_angles() # True

+ Referring to memeber variables

class Car(object):
    condition = "new"
    
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
        
my_car = Car("DeLorean", "silver", 88)

print my_car.model
print my_car.color
print my_car.mpg

+ Creating class methods

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    # add class method
    def display_car(self):
        return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
        
        
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg

print my_car.display_car()

+ Modifying member variables

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    # add class method
    def display_car(self):
        return "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)
    
    # change condition value
    def drive_car(self):
        self.condition = "used"
        
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg

print my_car.condition # before
print my_car.drive_car() # change condition
print my_car.condition # after

Viewing all articles
Browse latest Browse all 44

Trending Articles