728x90

2023-11-07 44th Class

Why Class?

#️⃣ 클래스를 사용하는 이유

  • def: 새로운 함수
  • class: 새로운 data type
  • 데이터 타입을 커스터마이징해서 필요한 메서드를 작성하여 사용하기위함

datatype 확인하기

test_int = 10  
test_float = 3.14  
  
test_list = {1, 2, 3}  
test_dict = {'a': 10, 'b': 20}  
  
print(f"test_int: {type(test_int)}")  
print(f"test_int: {type(test_float)}")  
print(f"test_int: {type(test_list)}")  
print(f"test_int: {type(test_dict)}")

'''
test_int: <class 'int'>
test_int: <class 'float'>
test_int: <class 'set'>
test_int: <class 'dict'>
'''

클래스의 함수에 파라미터 사용하기

class Person:  
    def say_hello(self, name):  
        print("Hello!", name)  
  
    def say_bye(self, name):  
        print("GoodBye!", name)  
  
  
person = Person()  
person.say_bye("Kim")  
person.say_bye("Yang")

'''
GoodBye! Kim
GoodBye! Yang
'''

클래스에 setter 사용하기

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
'''

setter로 설정한 attr 사용하기

class Person:  
    def set_name(self, name):  
        self.name = name  
  
    def say_hello(self):  
        print(f"Hello! I'm {self.name}")  
  
person1, person2 = Person(), Person()  
  
person1.set_name('Kim')  
person2.set_name('Yang')  
  
print(person1.name)  
print(person2.name)  
  
person1.say_hello()  
person2.say_hello()

'''
Kim
Yang
Hello! I'm Kim
Hello! I'm Yang
'''

setter로 설정한 attr 값을 활용하여 return 하기

class Person:
    def set_name(self, name):
        self.name = name

    def say_hello(self):
        print(f"Hello! I'm {self.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())

'''
김철수  
김  
철수
'''

init method 사용하기

class Person:  
    def __init__(self, name):  
        self.name = name  
  
  
person1 = Person("Yang")  
person2 = Person("Shin")  
  
print(person1.name)  
print(person2.name)

'''
Yang
Shin
'''

class 내부 함수를 init에서 call 하기

class Person:  
    def __init__(self, name):  
        self.name = name  
        self.say_hello()  
  
    def say_hello(self):  
        print(f"Hello! I'm {self.name}")  
  
person1 = Person("Yang")  
person2 = Person("Shin")

'''
Hello! I'm Yang
Hello! I'm Shin
'''

Logic Gate using Python Class

#️⃣ 파이썬 클래스를 사용하여 로직 게이트 구현하기

  1. 베이스 LogicGate
  2. Linear (AND, NAND, OR, NOR) 만들기
  3. Non-Linear (XOR, XNOR) 만들기
  4. special method (init, call)을 사용하여 만들기
  5. validation code로 확인하기

code

class LogicGate:  
    def __init__(self, w1, w2, b):  
        self.w1 = w1  
        self.w2 = w2  
        self.b = b  
  
    def __call__(self, x1, x2):  
        return 1 if (self.w1 * x1 + self.w2 * x2 + self.b) > 0 else 0  
  
  
class ANDGate:  
    def __init__(self):  
        self.gate = LogicGate(0.5, 0.5, -0.7)  
  
    def __call__(self, x1, x2):  
        return self.gate(x1, x2)  
  
class NANDGate:  
    def __init__(self):  
        self.gate = LogicGate(-0.5, -0.5, 0.7)  
  
    def __call__(self, x1, x2):  
        return self.gate(x1, x2)  
  
  
class ORGate:  
    def __init__(self):  
        self.gate = LogicGate(0.5, 0.5, -0.2)  
  
    def __call__(self, x1, x2):  
        return self.gate(x1, x2)  
  
  
class NORGate:  
    def __init__(self):  
        self.gate = LogicGate(-0.5, -0.5, 0.2)  
  
    def __call__(self, x1, x2):  
        return self.gate(x1, x2)  
  
  
class XORGate:  
    def __init__(self):  
        self.nand_gate = NANDGate()  
        self.or_gate = ORGate()  
        self.and_gate = ANDGate()  
  
    def __call__(self, x1, x2):  
        p = self.nand_gate(x1, x2)  
        q = self.or_gate(x1, x2)  
        r = self.and_gate(p, q)  
        return r  
  
  
class XNORGate:  
    def __init__(self):  
        self.nor_gate = NORGate()  
        self.and_gate = ANDGate()  
        self.or_gate = ORGate()  
  
    def __call__(self, x1, x2):  
        p = self.nor_gate(x1, x2)  
        q = self.and_gate(x1, x2)  
        r = self.or_gate(p, q)  
        return r  
  
  
  
if __name__ == '__main__':  
    and_gate = ANDGate()  
    nand_gate = NANDGate()  
    or_gate = ORGate()  
    nor_gate = NORGate()  
    xor_gate = XORGate()  
    xnor_gate = XNORGate()  
  
    print("========== 1 LAYER (LINEAR) ==========")  
    print(f"{and_gate(1, 1) = }")  
    print(f"{nand_gate(1, 1) = }")  
    print(f"{or_gate(0, 0) = }")  
    print(f"{nor_gate(0, 0) = }")  
  
    print("========== XOR ==========")  
    print(f"{xor_gate(0, 0) = }")  
    print(f"{xor_gate(0, 1) = }")  
    print(f"{xor_gate(1, 0) = }")  
    print(f"{xor_gate(1, 1) = }")  
  
    print("========== XNOR ==========")  
    print(f"{xnor_gate(0, 0) = }")  
    print(f"{xnor_gate(0, 1) = }")  
    print(f"{xnor_gate(1, 0) = }")  
    print(f"{xnor_gate(1, 1) = }")  
  
    print("========== VALIDATION TEST ==========")  
    to_test = [(0, 0), (0, 1), (1, 0), (1, 1)]  
    xor_target = [0, 1, 1, 0]  
    xnor_target = [1, 0, 0, 1]  
  
    for test, xor, xnor in zip(to_test, xor_target, xnor_target):  
        xor_val = xor_gate(*test)  
        xnor_val = xnor_gate(*test)  
  
        print(f"{test=} {xor_val=} ({xor_val == xor}), {xnor_val=} ({xnor_val == xnor})")

result

========== 1 LAYER (LINEAR) ==========
and_gate(1, 1) = 1
nand_gate(1, 1) = 0
or_gate(0, 0) = 0
nor_gate(0, 0) = 1
========== XOR ==========
xor_gate(0, 0) = 0
xor_gate(0, 1) = 1
xor_gate(1, 0) = 1
xor_gate(1, 1) = 0
========== XNOR ==========
xnor_gate(0, 0) = 1
xnor_gate(0, 1) = 0
xnor_gate(1, 0) = 0
xnor_gate(1, 1) = 1
========== VALIDATION TEST ==========
test=(0, 0) xor_val=0 (True), xnor_val=1 (True)
test=(0, 1) xor_val=1 (True), xnor_val=0 (True)
test=(1, 0) xor_val=1 (True), xnor_val=0 (True)
test=(1, 1) xor_val=0 (True), xnor_val=1 (True)

반응형