Class defines type, while instances are used in the code.
Name | Definition | Example |
---|---|---|
Class | A type w/ attribute and method | - fruits (attribute: color, taste; method: become juice, become dessert) - List (attribute: items; method: append, pop, …) |
Object (== Instance) | Actual existing instances used in code | - Apple, Pear, Orange, … - [1, 2, 3], [“Hello”, “World”] |
Defining class
class Person:
pass
Taeyeon = Person()
Yoona = Person()
a = list()
b = dict()
print(type(Taeyeon)) # <class '__main__.Person'>
class Person:
def __init__(self):
print(self, 'is generated')
self.name = 'Kate' # set attributes in constructor
self.age = 10
Taeyeon = Person() # <__main__.Person object at 0x12341234> is generated
class Person:
def __init__(self, name, a):
print(self, 'is generated')
self.name = name
self.age = a # same naming is recommanded
snsd1 = Person('Taeyeon', 21) # <__main__.Person object at 0x12341234> is generated
snsd1.name # Taeyeon
snsd1.age # 21
class Person:
def __init__(self, name, a):
print(self, 'is generated')
self.name = name
self.age = a
def dance(self):
print(self.name, 'is dancing')
snsd1 = Person('Taeyeon', 21) # <__main__.Person object at 0x12341234> is generated
snsd1.dance() # Taeyeon is dancing
'''
static method:
Does not have attribute --> self don't do anything
- Do not keep any data in class
- Only process any data passed into class
'''
class Math:
# explicitly tell static method decorator
# therefore, do not need self param
@staticmethod
def add(a, b):
return a + b
@staticmethod
def mult(a, b):
return a * b
#m = Math()
#m.add(10, 20) # 30
#m.mult(10, 20) # 200
# Use it like function
Math.add(10, 20) # 30
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{} is eating {}.'.format(self.name, food))
def drink(self, drink):
print('{} is drinking {}.'.format(self.name, drink))
def work(self, minute):
print('{} is working {} minutes.'.format(self.name, minute))
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
ef __init__(self, name, age):
self.name = name
self.age = age
#ty = Person('Taeyeon', 21)
ty = Student('Taeyeon', 21)
ty.eat('Chicken') # Taeyeon is eating Chicken.
ty.drink('Whisky')
ty.work('240')
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{} is eating {}.'.format(self.name, food))
def drink(self, drink):
print('{} is drinking {}.'.format(self.name, drink))
def work(self, minute):
print('{} is working {} minutes.'.format(self.name, minute))
class Idol(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
print('{} is singing {} minutes.'.format(self.name, minute))
class Employee(Person):
ef __init__(self, name, age):
self.name = name
self.age = age
#ty = Person('Taeyeon', 21)
# ty.work('240') # is working 240 minutes
ty = Idol('Taeyeon', 21)
ty.work('240') # is singing 240 minutes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{} is eating {}.'.format(self.name, food))
def drink(self, drink):
print('{} is drinking {}.'.format(self.name, drink))
def work(self, minute):
print('{} is working {} minutes.'.format(self.name, minute))
class Idol(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
super().work(minute)
print('{} is singing {} minutes.'.format(self.name, minute))
#ty = Person('Taeyeon', 21)
# ty.work('240') # is working 240 minutes
ty = Idol('Taeyeon', 21)
ty.work('240')'''Taeyeon is working 240 minutes.
Taeyeon is singing 240 minutes.'''
# Point
# 2d coord system's point (x, y)
# operation
# 2 points' addition, subtraction
# 1 point * n
# distance from (0, 0)
# get x, y
# print
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
# def add(self, pt):
# return Point(self.x + pt.x, self.y + pt.y)
def __add__(self, pt):
return self.x + pt.x, self.y + pt.y
# def print_pt(self):
# print('({},{})'.format(self.x, self.y))
def __str__(self):
return '({},{})'.format(self.x, self.y)
def __sub__(self, pt):
return Point(self.x - pt.x, self.y - pt.y)
def __mul__(self, factor):
return Point(self.x * factor, self.y * factor)
# def get_x(self):
# return self.x
# def get_y(self):
# return self.y
def __getitem__(self, index): # allow indexing
if index == 0:
return self.x
elif index == 1:
return self.y
else:
return -1
p1 = Point(3, 4)
p2 = Point(2, 7)
# p1.print_pt()
# p2.print_pt()
print(p1) # (3, 4)
print(p2) # (2, 7)
# p3 = p1.add(p2)
p3 = p1 + p2
print(p3) # (5, 11)
print(p1*2) # (6, 8)
print(p1[0])