[Python] decorate
·
기타
Decorate는 특정 함수에 다른 함수를 추가하는 기능입니다. 이를 통해 함수를 간단하게 확장하고 재사용할 수 있습니다. 예시코드 def decorator_function(func): def wrapper(): print("데코레이터 함수가 호출되기 전입니다.") func() print("데코레이터 함수가 호출된 후입니다.") return wrapper @decorator_function def print_hello(): print("Hello") print_hello() # 출력 결과 # 데코레이터 함수가 호출되기 전입니다. # Hello # 데코레이터 함수가 호출된 후입니다.
[Python] Visibility
·
기타
Visibility는 클래스 내부에서 정의된 멤버 변수와 메소드의 접근 권한을 의미합니다. 예시코드 class MyClass: def __init__(self): # public 멤버 변수 self.public_variable = 10 # private 멤버 변수 self.__private_variable = 20 # public 메소드 def public_method(self): print(self.public_variable) # private 메소드 def __private_method(self): print(self.__private_variable) # public 멤버 변수 접근 가능 my_instance = MyClass() print(my_instance.public_variable) # ..
[Python] polymorphism
·
기타
polymorphism은 같은 이름의 함수가 다른 기능을 수행할 수 있는 개념이다. 예시 코드: class Animal: def __init__(self, name): self.name = name def sound(self): pass # 이 함수는 모든 동물들의 소리를 내기 위해 사용될 것이다. class Dog(Animal): def sound(self): print('Woof!') # Dog 객체에 대한 sound 함수가 정의되었다. class Cat(Animal): def sound(self): print('Meow!') # Cat 객체에 대한 sound 함수가 정의되었다. dog = Dog('Max') cat = Cat('Luna') dog.sound() # Woof! cat.sound() #..
[Python] inheritance
·
기타
Inheritance는 기존의 클래스를 바탕으로 새로운 클래스를 생성하는 개념입니다. 상속받은 클래스는 부모 클래스의 속성과 메소드를 물려받게 되고, 필요에 따라 자식 클래스에서 추가하거나 수정할 수 있습니다. 예시코드 Parent 클래스를 상속받아 Child 클래스를 구현하고, 그 Child 클래스로부터 객체를 생성하여 객체의 value1과 value3 속성값을 출력하는 것입니다. # Parent 클래스 정의 - value1과 value2 속성을 초기화 class Parent: def __init__(self): self.value1 = "I'm parent!" self.value2 = "I'm parent, too!" # Child 클래스 정의 - Parent 클래스를 상속받아 value3 속성을 초..