Python

Getting Started

    Corey Schafer, Object-Oriented Programming 6 parts
    Setting up a Python Development Environment
      Requirements: syntax highlighting, beautification, linter, IntelliSense: list members, parameter info, quick info, autocompletion,
      ​Corey Schafer​

Class

    Why? Logically group our data and functions that we can easily reuse or build upon
    Terminology
      Attributes and methods - functions that is associated with class
      A class is a blueprint for creating instances
1
class Employee:
2
pass
3
​
4
# instances of a Employee classes
5
emp_1 = Employee()
6
emp_2 = Employee()
7
s
8
# unique data
9
#manually variable setting - prone to human error
10
emp_1.first = 'Corey'
11
emp_1.last = 'Scafer'
12
emp_1.email = '[email protected]'
13
emp_1.pay = '50000'
14
​
15
emp_2.first = 'Test'
16
emp_2.last = 'User'
17
emp_2.email = '[email protected]'
18
emp_21.pay = '60000'
19
######
20
​
21
class Employee:
22
def __init__(self,first,last,pay):
23
#set instance variable
24
self.first = first
25
self.last = last
26
self.pay =pay
27
self.email = first +'.' + last + '@company.com'
28
29
# each method within a class automatically takes the insance as the first argument
30
# and we always call that self
31
def fullname(self)
32
return '{}{}'.format(self.first,self.last)
33
34
# better solution: runs init automatically
35
emp_1 = Employee('Corey','Scafer',50000)
36
emp_2 = Employee('Test','User',60000)
37
​
38
print(emp_1.email)
39
print(emp_2.email)
40
​
41
# running actions
42
#manually
43
print('{}{}'.format)emp_1.first,emp_1,last))
44
45
# better
46
print(emp_1.fullname()) # notice the parenthesis as it is a method
47
​
48
#notes
49
#classes are kinda like function
50
# remember to put self in methods in class
51
emp_1.fullname() is same as Employee.fullname(emp_1))
Copied!
Last modified 8mo ago
Copy link