클래스

Lipton·2021년 12월 8일

알고리즘

목록 보기
3/6
class Person:
	pass

person_1 = Person()
print(person_1)
person_2 = Person()
print(person_2)

<__main__.Person object at 0x000001F6D97806D0>
<__main__.Person object at 0x000001F6D99BA670>


class Person:
	# 클래스는 컨스트럭터 생성해 줘야 함
    def __init__(self):
        print("i am created!", self)

person_1 = Person()
print(person_1)
person_2 = Person()
print(person_2)

i am created! <__main__.Person object at 0x00000187FB1B06D0>
<__main__.Person object at 0x00000187FB1B06D0>
i am created! <__main__.Person object at 0x00000187FB3EA670>
<__main__.Person object at 0x00000187FB3EA670>

파이썬은 컨스트럭터 생성하거나 내부에 있는 함수를 만들 때 인자에 자기 자신을 넘겨주게 되어 있다. 그것이 self 이다. 파이썬 클래스가 알아서 자기 자신을 넘겨준다.


class Person:
	# 클래스는 컨스트럭터 생성해 줘야 함
    def __init__(self, param_name):
        self.name = param_name

    def talk(self):
        print("hi, my name is", self.name)

person_1 = Person("Kim")
person_1.talk()
# print(person_1)
person_2 = Person("Park")
person_2.talk()
# print(person_2.name)

hi, my name is Kim
hi, my name is Park

profile
Web Frontend

0개의 댓글