210412 TIL


TIL


オペレーティングシステムの学習




深化する

class Student():
   """
   Student Class
   Author: sdk
   Date: 2021.04.12 
   """

   # 클래스 변수 
   student_count = 0 

   def __init__(self, name, number, grade, details, email=None):
       # 인스턴스 변수 
       self._name = name
       self._number = number
       self._grade = grade
       self._details = details
       self._email = email
       # 클래스 변수에 접근하기 
       Student.student_count += 1 

   def __str__(self):
       return 'str {}'.format(self._name)

   def __repr__(self):
       return 'repr {}'.format(self._name)

   def detail_info(self):
       print('Current Id : {}'.format(id(self)))
       print('student Detail Info : {} {} {}'.format(self._name, self._email, self._details))        

   def __del__(self):
       Student.student_count -= 1 

# self의 의미
studt1 = Student('cho', 2 ,3, {'gender': 'Male', 'score1': 65, 'score2': 44})
studt2 = Student('chang', 4 ,1, {'gender': 'Female', 'score1': 85, 'score2': 74}, '[email protected]')

# 아이디 값이 다르다. 
print(id(studt1))  # 140202496724944
print(id(studt2))  # 140202496724848

# is 는 아이디 값을 비교, == 는 값을 비교 
print(studt1._name == studt2._name) # False
print(studt1 is studt2) # False

# dir & __dict__ 확인
print(dir(studt1))  #['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_details', '_email', '_grade', '_name', '_number', 'detail_info', 'student_count']
print(dir(studt2)) 
print(studt1.__dict__) 
# {'_name': 'cho', '_number': 2, '_grade': 3, '_details': {'gender': 'Male', 'score1': 65, 'score2': 44}, '_email': None}
print(studt2.__dict__)

# Doctring - 주석을 출력 
print(Student.__doc__)
#   Student Class
#    Author: sdk
#    Date: 2021.04.12
print()

# 실행 
studt1.detail_info()
# Current Id : 140202496724944
# student Detail Info : cho None {'gender': 'Male', 'score1': 65, 'score2': 44}
studt2.detail_info()

# 에러 
Student.detail_info()


  • インスタンス変数とクラス変数の違いを理解する
  • dir()関数と__dict__の違いを理解する
  • Student.detail_info()でエラーが発生した理由を理解します(パラメータがないため)

    ショッピングモールの作成



  • ユーザ,商品,注文の3つのappを生成した.

  • それぞれモデルを構築した.
  • # 유저 모델 
    class Fcuser(models.Model):
        email = models.EmailField(verbose_name='이메일')
        password = models.CharField(max_length=64, verbose_name='비밀번호')
        register_date = models.DateTimeField(auto_now_add=True, verbose_name='생성날짜')
    
        def __str__(self):
            return self.email
    
        class Meta:
            db_table = 'products_user'
            verbose_name = '사용자'
            verbose_name_plural = '사용자'
    
    # 상품 모델 
    class Product(models.Model):
        name = models.CharField(max_length=256, verbose_name='상품명')
        price = models.IntegerField(verbose_name='상품가격')
        description = models.TextField(verbose_name='상품설명')
        stuck = models.IntegerField(verbose_name='재고')
        register_date = models.DateTimeField(auto_now_add=True, verbose_name='등록날짜')
    
        def __str__(self):
            return self.name
    
        class Meta:
            db_table = 'products_product'
            verbose_name = '상품'
            verbose_name_plural = '상품'
            
    # 주문 모델 
    
    class Order(models.Model):
        fcuser = models.ForeignKey('fcuser.Fcuser', on_delete=models.CASCADE, verbose_name='사용자')
        product = models.ForeignKey('product.Product', on_delete=models.CASCADE, verbose_name='상품')
        quantity = models.IntegerField(verbose_name='수량')
        register_date = models.DateTimeField(auto_now_add=True, verbose_name='등록날짜')
    
        def __str__(self):
            return str(self.fcuser) + ' ' + str(self.product)
    
        class Meta:
            db_table = 'products_order'
            verbose_name = '주문'
            verbose_name_plural = '주문'