본문 바로가기

Python Object-oriented Programming49

__eq__ Method 출처 : https://www.pythontutorial.net/python-oop/python-__eq__/ Introduction to the Python __eq__ method class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age 그리고 Person 클래스의 두 인스턴스를 만듭니다. john = Person('John', 'Doe', 25) jane = Person('Jane', 'Doe', 25) 이 예제에서 john 객체 와 jane 객체는 동일한 객체가 아닙니다. 그리고 is 연산자를 사용하여 확인할 수 있습니.. 2023. 4. 3.
__repr__ Method 출처 : https://www.pythontutorial.net/python-oop/python-__repr__/ Introduction to the Python __repr__ magic method __repr__ dunder 메서드는 클래스의 인스턴스를 repr()에 전달할 때의 동작을 정의합니다. __repr__ 메서드는 개체의 문자열 표현을 반환합니다. 일반적으로 __repr__()는 실행할 수 있고 객체와 동일한 값을 생성할 수 있는 문자열을 반환합니다. 즉, object_name.__repr__() 메서드의 반환된 문자열을 eval() 함수에 전달하면 object_name과 동일한 값을 얻게 됩니다. 예를 살펴보겠습니다. 먼저 세 가지 인스턴스 속성( first_name, last_name .. 2023. 4. 3.
__str__ Method 출처 : https://www.pythontutorial.net/python-oop/python-__str__/ Introduction to the Python __str__ method class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age person = Person('John', 'Doe', 25) print(person) # print(Person) # print() 함수를 사용하여 Person 클래스의 인스턴스를 표시하는 경우 print() 함수는 해당 인스턴스의 메모리 주소를 표시합니다. 때로는 클래스 인스턴스의 .. 2023. 4. 3.
Private Attributes 출처 : https://www.pythontutorial.net/python-oop/python-private-attributes/ Introduction to encapsulation in Python 캡슐화는 추상화, 캡슐화, 상속 및 다형성을 포함한 객체 지향 프로그래밍의 네 가지 기본 개념 중 하나입니다. 캡슐화는 단일 개체 내에서 해당 데이터에 대해 작동하는 데이터 및 함수의 packing입니다. 이렇게 하면 개체의 내부 상태를 외부에서 숨길 수 있습니다. 이를 정보 은닉이라고 합니다. 클래스는 캡슐화의 예입니다. 클래스는 데이터와 메서드를 단일 단위로 묶습니다. 그리고 클래스는 메서드를 통해 해당 속성에 대한 액세스를 제공합니다. 정보 숨기기의 개념은 외부에 보이지 않는 속성이있는 경우 해당 .. 2023. 4. 3.
Instance Variables 출처 : https://www.pythontutorial.net/python-oop/python-instance-variables/ Introduction to the Python instance variables Python에서 클래스 변수는 클래스에 바인딩되고 인스턴스 변수는 클래스의 속성 인스턴스에 바인딩됩니다. 인스턴스 변수는 인스턴스 속성이라고도 합니다. 다음은 두 개의 클래스 변수가 있는 HtmlDocument 클래스를 정의합니다. from pprint import pprint class HtmlDocument: version = 5 extension = 'html' pprint(HtmlDocument.__dict__) print(HtmlDocument.extension) print(HtmlD.. 2023. 4. 3.
__init__ Method 출처 : https://www.pythontutorial.net/python-oop/python-__init__/ Introduction to the Python __init__() method 클래스의 새 객체를 생성할 때 Python은 자동으로 __init__() 메서드를 호출하여 객체의 속성을 초기화합니다. 일반 메서드와 달리 __init__() 메서드에는 양쪽에 두 개의 밑줄(__)이 있습니다. 명명은 dunder init라고 합니다. __init__() 메서드의 양쪽에 있는 이중 밑줄은 파이썬이 내부적으로 메서드를 사용할 것임을 나타냅니다. 즉, 이 메서드를 명시적으로 호출하면 안 됩니다. 파이썬은 새 객체를 만든 직후에 __init__() 메서드를 자동으로 호출하기 때문에 __init__() .. 2023. 4. 3.
Class Methods Class Methods Introduction to Python class methods 지금까지 클래스의 특정 인스턴스에 바인딩된 인스턴스 메서드에 대해 알아보았습니다. 인스턴스 메서드는 동일한 클래스 내의 인스턴스 변수에 액세스할 수 있습니다. 인스턴스 메서드를 호출하려면 먼저 클래스의 인스턴스를 만들어야 합니다. 다음은 Person 클래스를 정의합니다. class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def get_full_name(self): return f"{self.first_name} {self.last.. 2023. 4. 2.
Methods 출처 : https://www.pythontutorial.net/python-oop/python-methods/ Introduction to the Python methods 정의에 따라 메서드는 클래스의 인스턴스에 바인딩된 함수입니다. 내부적으로 어떻게 작동하는지 이해하는 데 도움이 됩니다. 다음은 send() 함수를 포함하는 Request 클래스를 정의합니다. class Request: def send(): # 주의 : 인수로 self, cls 가 없다 print('Sent') 그리고 다음과 같이 Request 클래스를 통해 send() 함수를 호출할 수 있습니다. Request.send() # Sent send()는 함수 객체로, 다음 출력에 표시된 대로 함수 클래스의 인스턴스입니다. print(R.. 2023. 4. 2.
Class Variables 출처 : https://www.pythontutorial.net/python-oop/python-class-variables/ Introduction to the Python class variables 클래스를 포함해서 파이썬의 모든 것은 객체입니다. 즉, 클래스는 파이썬의 객체입니다. class 키워드를 사용하여 클래스를 정의하면 Python은 클래스 이름과 동일한 이름을 가진 객체를 만듭니다. 예를 들어: class HtmlDocument: pass 이 예제에서는 클래스와 개체를 정의합니다 . 객체에는 __name__ 속성이 있습니다. print(HtmlDocument.__name__) # HtmlDocument # instance 속성은 아니다. 그리고 HTMLDocument에서 보듯이 클래스의 .. 2023. 4. 2.