Python Class

phillip yoon·2021년 6월 24일
0

Class


OOP(객체 지향 프로그래밍)

클래스와 인스턴스 차이 이해

네임스페이스 : 객체를 인스턴스화 할 때 저장된 공간
클래스 변수 : 직접 접근 가능, 공유
인스턴스 변수 : 객체마다 별도 존재


💡Code



# 예제1
class Dog: # object 상속
    # 클래스 속성
    species = 'firstdog'

    # 초기화/인스턴스 속성
    def __init__(self,name,age):
        self.name = name
        self.age = age
#클래스 정보
print(Dog)
print()
# 인스턴스화
a = Dog("mikky",2)
b = Dog("baby",3)
# 비교
print(a == b, id(a),id(b))

print('dog1',a.__dict__)
print('dog2',b.__dict__)

# 인스턴스 속성 확인
print('{} is {} and {} is {}'.format(a.name,a.age,b.name,b.age))

if a.species == 'firstdog':
    print('{0} is a {1}'.format(a.name,a.species))
print(Dog.species)
print(a.species)
print(b.species)

# 예제 2
# self의 이해
class SelfTest:
    def func1():
        print("Func1 called")
    def func2(self):
        print(id(self))
        print("Func2 called")

f = SelfTest()

# print(dir(f))
print(id(f))
print()
f.func2()
print()
SelfTest.func1()
SelfTest.func2(f)

# 예제3
# 클래스 변수, 인스턴스 변수
class Warehouse:
    # 클래스 변수
    stock_num = 0 # 재고
    def __init__(self,name):
        self.name = name
        Warehouse.stock_num += 1
    def __del__(self):
        Warehouse.stock_num -= 1
user1 = Warehouse('Lee')
user2 = Warehouse('cho')

print(Warehouse.stock_num)
print(user1.name)
print(user2.name)
print(user1.__dict__)
print(user2.__dict__)
print('before',Warehouse.__dict__)
print('>>>', user1.stock_num)

del user1
print('after',Warehouse.__dict__)
print()
# 예제 4
class Dog2: # object 상속
    # 클래스 속성
    species = 'firstdog'

    # 초기화/인스턴스 속성
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def info(self):
        return '{} is {} years old.'.format(self.name,self.age)
    def speak(self,sound):
        return "{} says {}!".format(self.name,sound)

# 인스턴스 생성
c = Dog2('july',4)
d = Dog2('Marry',10)
print(c.info())
print(d.info())
print(c.speak('Wal Wal'))
print(d.speak('Mung Mung'))
profile
세상이 더 나아지기를 바라는 마음으로 개발에 임하고 있습니다.

0개의 댓글