πŸ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ꡬ문 였λ₯˜μ™€ μ˜ˆμ™Έ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • Constructor
  • μƒμ„±μž (Constructor)
  • μ†Œλ©Έμž (Destructor)

Was this helpful?

  1. python

Constructor

Constructor

μƒμ„±μž (Constructor)

  • 객체λ₯Ό 생성할 λ•Œ ν˜ΈμΆœλ˜λŠ” ν•¨μˆ˜λ‘œμ„œ, 객체 μƒμ„±μ‹œ μ΄ˆκΈ°ν™” μž‘μ—…μ„ μœ„ν•΄ 쑴재

class 클래슀λͺ…: 
    def __init__(self): 
        λ¬Έμž₯
  • λ‹€μŒκ³Ό 같이 initλΌλŠ” μ΄λ¦„μœΌλ‘œ 미리 μ •μ˜λ˜λ©°, μƒμ„±μžλ₯Ό ν†΅ν•΄μ„œ 객체가 μƒμ„±λ λ•Œ μ–΄λ–€ λ³€μˆ˜μ˜ 값을 μ„ΈνŒ…ν•˜λŠ” λ“± μ—¬λŸ¬κ°€μ§€ μž‘μ—…μ„ ν• μˆ˜ μžˆλ‹€

class example:
    def __init__(self, sentence): 
        self.name = sentence 
        print("객체가 생성: " + sentence + "μž…λ‹ˆλ‹€.") 

object= example("사과") # 객체가 생성: μ‚¬κ³Όμž…λ‹ˆλ‹€.
class Student:
    def __init__(self, name, age): 
        self.name = name
        self.age = age
    def introduction(self):
        print("제 이름은 " + self.name + "이며, 제 λ‚˜μ΄λŠ” " + str(self.age) + "μ‚΄μž…λ‹ˆλ‹€.")

objectStudent = Student("μ›λΉˆ", 28)
objectStudent.introduction() # 제 이름은 μ›λΉˆμ΄λ©°, 제 λ‚˜μ΄λŠ” 28μ‚΄μž…λ‹ˆλ‹€.

μ†Œλ©Έμž (Destructor)

  • μƒμ„±μžμ™€ λ°˜λŒ€λ‘œ 객체가 μ†Œλ©Έν• λ•Œ ν˜ΈμΆœλ˜λŠ” μ†Œλ©Έμžκ°€ μžˆλ‹€

  • del으둜 μ •μ˜ λ˜μ–΄ 있으며, λ¦¬μ†ŒμŠ€ 해체 λ“±μ˜ μ’…λ£Œ μž‘μ—…μ„ ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

class 클래슀λͺ…: 
    def __del__(self): 
        λ¬Έμž₯
class Student:
    def __init__(self, name, age): 
        self.name = name
        self.age = age
        print("객체 생성 : 제 이름은 " + self.name + "이며, 제 λ‚˜μ΄λŠ” " + str(self.age) + "μ‚΄μž…λ‹ˆλ‹€.")

    def __del__(self):
        print("객체 μ†Œλ©Έ")

objectStudent = Student("μ›λΉˆ", 28)
# 객체 생성 : 제 이름은 μ›λΉˆμ΄λ©°, 제 λ‚˜μ΄λŠ” 28μ‚΄μž…λ‹ˆλ‹€.
print(objectStudent) # <__main__.Student object at 0x0000029D72681F48>
del objectStudent
# 객체 μ†Œλ©Έ
# print(objectStudent) # name 'objectStudent' is not defined
PreviousbuiltinFunctionNextalgorithm

Last updated 3 years ago

Was this helpful?