<수업 내용>
Python Class
- 클래스를 구현하는 이유?
- 클래스는 object type을 말한다.
- 클래스를 만든다는 것은 object type을 만드는 것
- 우리가 필요한 method들을 넣어주고 커스터마이징 하는 것
class TestClass:
pass
object1=TestClass()
object2=TestClass()
print("Object1:", type(object1))
print("Object2:", type(object2))
>>
Object1: <class '__main__.TestClass'>
Object2: <class '__main__.TestClass'>
class Person:
def say_hello(self):
print("Hello")
def say_bye(self):
print("Good bye!")
person=Person()
person.say_hello()
>>
Hello
class Person:
def say_hello(self):
print("Hello")
def say_bye(self, first_name):
print("Good bye!", first_name)
person=Person()
person.say_bye("Kim")
person.say_bye("Yang")
>>
Good bye! Kim
Good bye! Yang
class Person:
def set_name(self, name):
self.name = name
person1, person2 = Person(), Person()
person1.set_name('Kim')
person2.set_name('Yang')
print(person1.name)
print(person2.name)
>>
Kim
Yang
class Person:
def set_name(self, name):
self.name = name
def say_hello(self):
print("Hello! I'm", self.name)
person1, person2 = Person(), Person()
person1.set_name('Kim')
person2.set_name('Yang')
person1.say_hello()
person2.say_hello()
print(person1.name)
print(person2.name)
>>
Hello! I'm Kim
Hello! I'm Yang
Kim
Yang
class Person:
def set_name(self, name):
self.name = name
def get_name(self):
return self.name
def get_family_name(self):
return self.name[0]
def get_personal_name(self):
return self.name[1:]
person = Person()
person.set_name("김철수")
print(person.get_name())
print(person.get_family_name())
print(person.get_personal_name())
>>
김철수
김
철수
class Person:
def __init__(self):
print("Person object instantiated")
person1 = Person()
person2 = Person()
>>
Person object instantiated
Person object instantiated
class Person:
def __init__(self, name):
self.name = name
self.say_hello()
def say_hello(self):
print("Hello! I'm", self.name)
person1 = Person("Yang")
person2 = Person("Shin")
Logicgate를 class로 구현하기
class LogicGate:
def __init__(self, weight_1, weight_2, bias):
self.weight_1 = weight_1
self.weight_2 = weight_2
self.bias = bias
def forward(self, x_1, x_2):
linear_creterion = self.weight_1*x_1 + self.weight_2*x_2 + self.bias
if linear_creterion > 0:
return 1
else: return 0
class AndGate:
def __init__(self):
self.gate = LogicGate(weight_1=1, weight_2=1, bias=-1.5)
def forward(self, x_1, x_2):
return self.gate.forward(x_1, x_2)
class OrGate:
def __init__(self):
self.gate = LogicGate(weight_1=1, weight_2=1, bias=-0.5)
def forward(self, x_1, x_2):
return self.gate.forward(x_1, x_2)
class NandGate:
def __init__(self):
self.gate = LogicGate(weight_1=-1, weight_2=-1, bias=1.5)
def forward(self, x_1, x_2):
return self.gate.forward(x_1, x_2)
class NorGate:
def __init__(self):
self.gate = LogicGate(weight_1 = -1, weight_2 = -1, bias = 0.5)
def forward(self, x_1, x_2):
return self.gate.forward(x_1, x_2)
class XorGate:
def __init__(self):
self.or_gate = OrGate()
self.nand_gate = NandGate()
self.and_gate = AndGate()
def forward(self, x_1, x_2):
return self.and_gate.forward(self.or_gate.forward(x_1, x_2), self.nand_gate.forward(x_1, x_2))
class XnorGate:
def __init__(self):
self.or_gate = OrGate()
self.nor_gate = NorGate()
self.and_gate = AndGate()
def forward(self, x_1, x_2):
return self.or_gate.forward(self.and_gate.forward(x_1, x_2), self.nor_gate.forward(x_1, x_2))
class XnorGate:
def __init__(self):
self.xor_gate = XorGate()
def forward(self, x_1, x_2):
return int(not(self.xor_gate.forward(x_1, x_2)))
if __name__ == '__main__':
xor_gate = XorGate()
print(xor_gate.forward(0,0))
print(xor_gate.forward(0,1))
print(xor_gate.forward(1,0))
print(xor_gate.forward(1,1))
>>
0
1
1
0
class LogicGate:
def __init__(self, weight_1, weight_2, bias):
self.weight_1 = weight_1
self.weight_2 = weight_2
self.bias = bias
def __call__(self, x_1, x_2):
linear_creterion = self.weight_1*x_1 + self.weight_2*x_2 + self.bias
if linear_creterion > 0:
return 1
else: return 0
class AndGate:
def __init__(self):
self.gate = LogicGate(weight_1=1, weight_2=1, bias=-1.5)
def __call__(self, x_1, x_2):
return self.gate(x_1, x_2)
class OrGate:
def __init__(self):
self.gate = LogicGate(weight_1=1, weight_2=1, bias=-0.5)
def __call__(self, x_1, x_2):
return self.gate(x_1, x_2)
class NandGate:
def __init__(self):
self.gate = LogicGate(weight_1=-1, weight_2=-1, bias=1.5)
def __call__(self, x_1, x_2):
return self.gate(x_1, x_2)
class NorGate:
def __init__(self):
self.gate = LogicGate(weight_1 = -1, weight_2 = -1, bias = 0.5)
def __call__(self, x_1, x_2):
return self.gate(x_1, x_2)
class XorGate:
def __init__(self):
self.or_gate = OrGate()
self.nand_gate = NandGate()
self.and_gate = AndGate()
def __call__(self, x_1, x_2):
return self.and_gate(self.or_gate(x_1, x_2), self.nand_gate(x_1, x_2))
class XnorGate:
def __init__(self):
self.or_gate = OrGate()
self.nor_gate = NorGate()
self.and_gate = AndGate()
def __call__(self, x_1, x_2):
return self.or_gate(self.and_gate(x_1, x_2), self.nor_gate(x_1, x_2))
if __name__ == '__main__':
xor_gate = XorGate()
print(xor_gate(0,0))
print(xor_gate(0,1))
print(xor_gate(1,0))
print(xor_gate(1,1))
>>
0
1
1
0
- init : 객체 생성시 자동으로 호출되는 메서드
- call : 객체에 괄호만 붙여서 호출 할 수 있는 메서드. 보통 가장 당연하게 생각되는 대표적, 기본적 메서드를 지정한다