Deep Learning/Pytorch

[Python] Class

jstar0525 2022. 1. 13. 17:14
반응형

딥러닝에서 python을 이용하여 다양하게 프로그램을 할 수 있지만

Class를 만들어 관리하는 것이 간편하고 중요하다.

 

이에 대하여 간단히 알아본다.

 

* 참고 : 이 글은 colab의 .ipynb 파일을 통하여 실험하였습니다.

 

Class and objects

  • Class
    • Class is a frame which can create identical things
  • Object
    • Things generated by a class

 

Features of Python Class

  • In object-oriented programming, the composition of the program is made around objects
  • A class is a frame that defines the properties and methods used in an object
    • attribute
    • method
  • Multiple objects can be created from a class, and each object can be handled independently

 

Features of objects created by classes

  • Each object has its own characteristics
  • Objects created by a class do not affect each other

 

Implementation of methods in a class

FourCal class for arithmetic operations

1. 클래스 구조 만들기

class FourCal:
    pass
a = FourCal()
print(type(a))  # <class '__main__.FourCal'>

2. 객체에 숫자 지정할 수 있게 만들기

class FourCal:
    def setdata(self, first, second):
        self.first = first
        self.second = second
a = FourCal()
a.setdata(4,2)
print(a.first)  # 4
print(a.second)  # 2
a.first = 6
a.first  # 6
b = FourCal()
b.setdata(5,3)
b.first  # 5
print(id(a.first))  # 94358262749856
print(id(b.first))  # 94358262749824

3. 더하기, 곱하기, 빼기, 기능 만들기

class FourCal:
    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result
a = FourCal()
a.setdata(4,2)
print(a.add())  # 6
print(a.sub())  # 2
print(a.mul())  # 8
print(a.div())  # 2.0

 

Constructor

class FourCal:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def setdata(self, first, second):
        self.first = first
        self.second = second

    def add(self):
        result = self.first + self.second
        return result

    def sub(self):
        result = self.first - self.second
        return result

    def mul(self):
        result = self.first * self.second
        return result

    def div(self):
        result = self.first / self.second
        return result
a = FourCal()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-5cfa1e393ba9> in <module>()
----> 1 a = FourCal()

TypeError: __init__() missing 2 required positional arguments: 'first' and 'second'
a = FourCal(5,2)
a.add()  # 7

 

Inheritance

class MoreFourCal(FourCal):
    def pow(self):
        result = self.first ** self.second
        return result
a = MoreFourCal(5,2)
print(a.add())  # 7
print(a.pow())  # 25

 

Method overriding

a = MoreFourCal(5,0)
a.div()

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-15-5b44b994db41> in <module>()
      1 a = MoreFourCal(5,0)
----> 2 a.div()

<ipython-input-10-43d46555f788> in div(self)
     21 
     22     def div(self):
---> 23         result = self.first / self.second
     24         return result

ZeroDivisionError: division by zero
class MoreFourCal(FourCal):
    def pow(self):
        result = self.first ** self.second
        return result

    def div(self):
        if self.second == 0:
            return 0
        else: 
            result = self.first / self.second
            return result
a = MoreFourCal(5,0)
a.div()  # 0

 

super()

class Person:
    def __init__(self):
        print("Person class")
        self.hello = 'hello'

class Student(Person):
    def __init__(self):
        print("Student class")
        self.school = "University"
james = Student()  # Student class
james.hello

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-da95e56173fd> in <module>()
----> 1 james.hello

AttributeError: 'Student' object has no attribute 'hello'
class Person:
    def __init__(self):
        print("Person class")
        self.hello = 'hello'

class Student(Person):
    def __init__(self):
        print("Student class")
        super().__init__()
        self.school = "University"
james = Student()
james.hello  # 'hello'

 

Class variable

class Family:
    lastname = "Kim"
Family.lastname  # 'Kim'
a = Family()
b = Family()
print(a.lastname)  # Kim
print(b.lastname)  # Kim
Family.lastname = "Han"
print(a.lastname)  # Han
print(b.lastname)  # Han
반응형