-
Notifications
You must be signed in to change notification settings - Fork 0
/
oop.py
139 lines (106 loc) · 4.23 KB
/
oop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# # How to define a class: CapitalizeAndUsePascalCase
# # PascalCase vs. camelCase vs snake_case
# class Car:
# pass
# def __init__(self):
# self.speed = 20 # default speed
# def race_mode(self):
# self.speed += 30
# return f"Race time! My new speed is {self.speed}mph"
# # Creating an instance (aka object) for User
# car_1 = Car()
# # Adding attributes (aka variables) to an object:
# car_1.seats = 5
# car_1.color = "blue"
# print(f"Car 1's color is :{car_1.color}")
# # Calling the race_mode METHOD on object car_1.
# print(car_1.race_mode())
# class User:
# # To initialize attributes (or set the starting values for attributes):
# # The __init__ function is called e/time a new object is created for a class:
# def __init__(self, user_id, username):
# self.id = user_id
# self.username = username
# self.followers = 0 # This is being given a default value
# # Default values do not need to be inluded as a parameter when making a new object
# # Once parameters are added to class definition, they must be included
# # each time a new object is made.
# user_1 = User("001", "Jenny")
# print(user_1) # <__main__.User object at 0x10add5fd0>
# print(f"User 1's username is :{user_1.username}")
# # Learn homework:
# class BasketballTeam:
# def __init__(self):
# self.members = []
# def add_member(self, name):
# # self.members += [name]
# self.members.append(name)
# return True
# class Automobile:
# def __init__(self):
# self.speed = 0
# def accelerate(self, speed_delta):
# self.speed += speed_delta
# return self.speed
# def adjust_to_speed_limit(self, speed_limit):
# if speed_limit == self.speed :
# return self.accelerate(0)
# elif speed_limit - self.speed < 0:
# return self.accelerate(-1)
# elif speed_limit - self.speed > 5:
# return self.accelerate(2)
# else:
# return self.accelerate(1)
# ## Q 12:
# def get_leading_team(home_team, away_team):
# pass
# if self.get_total_score(self,home_team) > self.get_total_score(self,away_team):
# return self.home_team
# else:
# return self.away_team
# class BasketballTeam:
# def __init__(self, team_name, initial_score_dict):
# self.team_name = team_name
# self.score_dict = initial_score_dict
# def get_total_score(self):
# return sum(self.score_dict.values())
class Employee:
# class variable: a variable with scope across all instances of the class:
num_of_employees = 0
def __init__(self, first_name, last_name, pay=20000): # __init__ aka 'the constructor'
self.first_name = first_name
self.last_name = last_name
self.pay = pay
self.email = first_name + "." +last_name + '@company.com'
self.full_name = first_name +" "+ last_name
# This updates the # of employees with each new instance:
Employee.num_of_employees +=1
def calc_payraise(self, perc_inc):
new_pay = self.pay * (100+ perc_inc)/100
return print(f"Your new payrate is ${new_pay:.0f}. Congrats, you earned a {perc_inc}% raise!")
# Comany prior to adding employees:
print(Employee.num_of_employees)
# first instances of the class, made through instantiation
emp_1 = Employee('Heather', "Marks")
emp_2 = Employee('Tim', "Canner", 5000)
# after instantiating 2 employees:
print(Employee.num_of_employees)
print(emp_1.email)
print(emp_1.full_name)
print(f"{emp_2.first_name} {emp_2.last_name} makes {emp_2.pay} a year. That is our company's base pay")
# Call method beginning with the instance:
emp_2.calc_payraise(5)
# A different way to call this method, beginning with class, passing in the instance:
Employee.calc_payraise(emp_2,5)
# To print out a dictionary for the instance, with key:value pairs being instance variables:values)
print(emp_2.__dict__)
# output: {'first_name': 'Tim', 'last_name': 'Canner', 'pay': 5000,\
# 'email': '[email protected]', 'full_name': 'Tim Canner'}
@classmethod
def set_rasise_amt(cls, amount):
cls.raise_amt = amount
@staticmethod
def is_workday(day_weekday):
if day_weekday == 5 or day_weekday ==6:
return False
return True