5/14/2020

Python - OOP(Object-Oriented Programming) Classes and Instances

** OOP Tutorial 1: Classes and Instances


==========================================

# Python Object-Oriented Programming

class Employee:
pass


emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)

---------------------------------

__main__.Employee object at 0x022CFF88
__main__.Employee object at 0x022CFFB8

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:
pass


emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)


emp_1.first = 'HAN'
emp_1.last = 'SeokDu'
emp_1.email = 'HAN@company.com'
emp_1.pay = 50000


emp_2.first = 'KIM'
emp_2.last = 'Young'
emp_2.email = 'KIM@company.com'
emp_2.pay = 30000


print(emp_1.email)
print(emp_2.email)

---------------------------------

__main__.Employee object at 0x01D6FF88
__main__.Employee object at 0x01D6FFB8
HAN@company.com
KIM@company.com

---------------------------------
==========================================

# Python Object-Oriented Programming

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

print(emp_1.email)
print(emp_2.email)

---------------------------------

HAN.SeokDu@company.com
KIM.Young@company.com

---------------------------------

==========================================


# Python Object-Oriented Programming

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

print(emp_1.email)
print(emp_2.email)

print('{} {}'.format(emp_1.first, emp_1.last))

---------------------------------

HAN.SeokDu@company.com
KIM.Young@company.com
HAN SeokDu

---------------------------------

==========================================

# Python Object-Oriented Programming

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)


emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)



print(emp_1.email)
print(emp_2.email)

print(emp_1.fullname())

---------------------------------

HAN.SeokDu@company.com
KIM.Young@company.com
HAN SeokDu

---------------------------------

==========================================

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)


emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)


print(emp_2.fullname())

---------------------------------

KIM Young

---------------------------------

==========================================

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname():
return '{} {}'.format(self.first, self.last)


emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)


print(emp_2.fullname())

---------------------------------

print(emp_2.fullname())
TypeError: fullname() takes 0 positional arguments but 1 was given

---------------------------------

==========================================

class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'

def fullname(self):
return '{} {}'.format(self.first, self.last)


emp_1 = Employee('HAN', 'SeokDu', 50000)
emp_2 = Employee('KIM', 'Young', 30000)

emp_1.fullname()
print(Employee.fullname(emp_1))

---------------------------------

HAN SeokDu

---------------------------------