자신에게 친절할 것 :)

Web Developing/Python

[Python] Object Oriented Programming

Tashapark 2024. 4. 23. 02:40
728x90

// 노마드 코더의 웹 스크래퍼 파이썬 강의 듣는 중//

 

솔직히 설명은 자바의 정석이 더 세세하지만,

예제로 바로바로 변화로 보는 것은 니코의 설명이 훨씬 더 직관적이고,

어떻게 적용해야 할지가 보임. 

다만, 처음으로 OOP를 접하는 것이.. 이 강의라면,, 그것은 개념 이해에는 어렵게 느껴질 것으로 여겨짐. 

 


 

# class: 데이터를 정의하고 그 데이터를 기반으로 동작하는 함수를 정의하는 것을 도움.

 

class Puppy:
# method는 펑션 안에 있는 class
# class 안에 method를 가질 경우, 첫 번째 argument는 자동으로 self임.
    def __init__(self):
        self.name = "Ruffus"
        self.age = 0.1
        self.breed = "Beagle"

# self가 곧 루푸스임. 첫번째 argument라서 
# but, 밑에다가 bibi = Puppy()를 만들어도 출력은 계속 ruffus로 나옴
ruffus = Puppy()

print(ruffus.name, ruffus.age, ruffus.breed)

 

이해가..  쉽지가 않아ㅠ,

다음 영상 보니깐 나아짐 ㅎ

 

class Puppy:
# method는 펑션 안에 있는 class
# class 안에 method를 가질 경우, 첫 번째 argument는 자동으로 self임.
# self 이외에 계속 업데이트 될 변수를 파라미터로 넣어줌. 
    def __init__(self, name, breed):
        self.name = name
        self.age = 0.1
        self.breed = breed

# self가 곧 루푸스임. 첫번째 argument라서 
# but, 밑에다가 bibi = Puppy()를 만들어도 출력은 계속 ruffus로 나옴
# 위에서 넣어준 파라미터 값을 함수에 추가 해주면 됨. 
# 위치 argument 사용 중임. 
ruffus = Puppy(
    "Ruffus", 
    "Beagle"
    )
bibi = Puppy(
    "Bibi", 
    "Dalmatian"
    )

print(ruffus.name, bibi.name)

 

 

class Puppy:
# 데이터 설정 setting하고. 파라미터 꼭 넣어주기.
    def __init__(self, name, breed):
        self.name = name
        self.age = 0.1
        self.breed = breed
#데이터 접근 access 가능 --> 프로퍼티 커스터 마이징 가능
    def __str__(self):
        return f"{self.breed} puppy named {self.name}"

# ruffus, bibi는 puppy의 blueprint를 활용해서 만들어진 object로 instance임
ruffus = Puppy(
    name="Ruffus", 
    breed="Beagle"
    )
bibi = Puppy(
    name="Bibi", 
    breed="Dalmatian"
    )
# 하나씩 출력하는 게 아니라, 
# 전체를 출력하고 위에서 콘솔에 어떻게 보일지 property를 커스터 마이징 가능함 

print(bibi,
      ruffus,)

 

 

# 클래스로 코드가 안 날라다니게 할 수 있음. 
# 더 기능적임. 함수를 그냥 위로 안 쌓고 클래스에 넣으면 됨.

# inheritance는 반복을 하지 않게 도와줌. 

# 이러면 퍼피랑 이닛이 겹침 
class GuardDog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
        self.age = 3
    
    def rrrrr(self):
        print("stay away!")
class Puppy:
# 데이터 설정 setting하고. 파라미터 꼭 넣어주기.
    def __init__(self, name, breed):
        self.name = name
        self.age = 0.1
        self.breed = breed
#데이터 접근 access 가능 --> 프로퍼티 커스터 마이징 가능
    def __str__(self):
        return f"{self.breed} puppy named {self.name}"

    def woof_woof(self):
        print("Woof Woof!")

# method 안에 method를 부를 수 있음.
    def introduce(self):
        self.woof_woof()
        print(f"My name is {self.name} and I am a baby  {self.breed}")
        self.woof_woof()

# ruffus, bibi는 puppy의 blueprint를 활용해서 만들어진 object로 instance임
ruffus = Puppy(
    name="Ruffus", 
    breed="Beagle"
    )
bibi = Puppy(
    name="Bibi", 
    breed="Dalmatian"
    )
# 하나씩 출력하는 게 아니라, 
# 전체를 출력하고 위에서 콘솔에 어떻게 보일지 property를 커스터 마이징 가능함 

ruffus.introduce()
bibi.introduce()

 

 

# 클래스로 코드가 안 날라다니게 할 수 있음. 
# 더 기능적임. 함수를 그냥 위로 안 쌓고 클래스에 넣으면 됨.

# inheritance는 반복을 하지 않게 도와줌. 
# 부모로부터 다 받는 것.

class Dog:
     def __init__(self, name, breed):
        self.name = name
        self.breed = breed
        self.age = age

# (Dog)를 넣어주면 Dog를 전부 상속 받음
class GuardDog(Dog):
    
    def rrrrr(self):
        print("stay away!")

class Puppy(Dog):

#데이터 접근 access 가능 --> 프로퍼티 커스터 마이징 가능
    def __str__(self):
        return f"{self.breed} puppy named {self.name}"

    def woof_woof(self):
        print("Woof Woof!")

# method 안에 method를 부를 수 있음.
    def introduce(self):
        self.woof_woof()
        print(f"My name is {self.name} and I am a baby  {self.breed}")
        self.woof_woof()

# ruffus, bibi는 puppy의 blueprint를 활용해서 만들어진 object로 instance임
ruffus = Puppy(
    name="Ruffus", 
    breed="Beagle"
    )
bibi = Puppy(
    name="Bibi", 
    breed="Dalmatian"
    )
# 하나씩 출력하는 게 아니라, 
# 전체를 출력하고 위에서 콘솔에 어떻게 보일지 property를 커스터 마이징 가능함 

ruffus.introduce()
bibi.introduce()

 

 

# 클래스로 코드가 안 날라다니게 할 수 있음. 
# 더 기능적임. 함수를 그냥 위로 안 쌓고 클래스에 넣으면 됨.
# property와 data를 완벽하게 캡슐화 함

# inheritance는 반복을 하지 않게 도와줌. 
# 부모로부터 다 받는 것.

class Dog:
     def __init__(self, name, breed, age):
        self.name = name
        self.breed = breed
        self.age = age

     def sleep(self):
        print("zzzzzzzz..")

# (Dog)를 넣어주면 Dog를 전부 상속 받음.
#  Dog의 메소드 다 갖다가 쓸 수 있음.
class GuardDog(Dog):
 
    def __init__(self, name, breed):
        super().__init__(
            name, 
            breed, 
            3,
            )
        self.aggresive = True
        
    def rrrrr(self):
        print("stay away!")

# super().__init__ 부모 클래스를 참조하는 것 
# 변경되는 것은 값을 써주면 됨
#  당연히 부모에 포함되지 않는 것도 가질 수 있음. 
class Puppy(Dog):
    
    def __init__(self, name, breed):
        super().__init__(
            name, 
            breed, 
            0.1,
            )
        self.spoiled = True


    def woof_woof(self):
        print("Woof Woof!")


# ruffus, bibi는 puppy의 blueprint를 활용해서 만들어진 object로 instance임
ruffus = Puppy(
    name="Ruffus", 
    breed="Beagle"
    )
bibi = GuardDog(
    name="Bibi", 
    breed="Dalmatian"
    )
# 하나씩 출력하는 게 아니라, 
# 전체를 출력하고 위에서 콘솔에 어떻게 보일지 property를 커스터 마이징 가능함 

ruffus.sleep()
bibi.sleep()

 

728x90
반응형

'Web Developing > Python' 카테고리의 다른 글

[Python] sort, sorted 주의  (0) 2024.05.14
[Python] MAC VSCode pip 설치 안 됐을 때  (0) 2024.04.22