πŸ“—
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
  • getter/setter
  • getter/setter
  • 프라이빗 λ³€μˆ˜
  • λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•œ κ²Œμ»€μ™€ μ„Έν„° : @property 와 @λ³€μˆ˜μ΄λ¦„.setter
  • μ›μ˜ λ‘˜λ ˆμ™€ 넓이 : OOP

Was this helpful?

  1. python

getset

getter/setter

getter/setter

κ²Œν„°μ™€ μ„Έν„°λŠ” 프라이빗 λ³€μˆ˜μ˜ 값을 μΆ”μΆœν•˜κ±°λ‚˜ λ³€κ²½ν•  λͺ©μ μœΌλ‘œ, κ°„μ ‘μ μœΌλ‘œ 속성에 μ ‘κ·Όν•˜λ„λ‘ ν•΄μ£ΌλŠ” ν•¨μˆ˜

import math

class Circle:
    def __init__(self, radius):
        self.__radius = radius
    def get_circumference(self):
        return 2 * math.pi *  self.__radius
    def get_area(self):
        return math.pi * (self.__radius ** 2)
    def get_radius(self):
        return self.__radius
    def set_radius(self, value):
        self.__radius = value

circle = Circle(10)
print("# μ›μ˜ λ‘˜λ ˆμ™€ 넓이λ₯Ό κ΅¬ν•©λ‹ˆλ‹€.")
print("μ›μ˜ λ‘˜λ ˆ:", circle.get_circumference())
print("μ›μ˜ 넓이:", circle.get_area())
print("# __radius에 μ ‘κ·Όν•©λ‹ˆλ‹€.")
print(circle.get_radius())

프라이빗 λ³€μˆ˜

λ³€μˆ˜λ₯Ό λ§ˆμŒλŒ€λ‘œ μ‚¬μš©ν•˜λŠ” 것을 막기 μœ„ν•΄μ„œ, 클래슀 λ‚΄λΆ€μ˜ λ³€μˆ˜λ₯Ό μ™ΈλΆ€μ—μ„œ μ‚¬μš©ν•˜λŠ” 것을 막고 싢을 λ•Œ μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ 이름을 __λ³€μˆ˜μ΄λ¦„ 의 ν˜•νƒœλ‘œ μ„ μ–Έ

import math

class Circle:
    def __init__(self, radius):
        self.__radius = radius
    def get_circumference(self):
        return 2 * math.pi *  self.__radius
    def get_area(self):
        return math.pi * (self.__radius ** 2)

circle = Circle(10)
print("# μ›μ˜ λ‘˜λ ˆμ™€ 넓이λ₯Ό κ΅¬ν•©λ‹ˆλ‹€.")
print("μ›μ˜ λ‘˜λ ˆ:", circle.get_circumference())
print("μ›μ˜ 넓이:", circle.get_area())
print("# __radius에 μ ‘κ·Όν•©λ‹ˆλ‹€.")
print(circle.__radius)

λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•œ κ²Œμ»€μ™€ μ„Έν„° : @property 와 @λ³€μˆ˜μ΄λ¦„.setter

import math

class Circle:
    def __init__(self, radius):
        self.__radius = radius
    def get_circumference(self):
        return 2 * math.pi *  self.__radius
    def get_area(self):
        return math.pi * (self.__radius ** 2)

    @property 
    def radius(self):
        return self.__radius
    @radius.setter 
    def radius(self, value):
        if value <= 0:
            raise TypeError("κΈΈμ΄λŠ” μ–‘μ˜ μˆ«μžμ—¬μ•Ό ν•©λ‹ˆλ‹€.")
        self.__radius = value

print("# λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•œ Getter와 Setter")
circle = Circle(10)
print("μ›λž˜ μ›μ˜ λ°˜μ§€λ¦„: ", circle.radius)
circle.radius = 2
print("λ³€κ²½λœ μ›μ˜ λ°˜μ§€λ¦„: ", circle.radius)
print("# κ°•μ œλ‘œ μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚΅λ‹ˆλ‹€.")
circle.radius = -10

μ›μ˜ λ‘˜λ ˆμ™€ 넓이 : OOP

import math

class Circle:
    def __init__(self, radius):
        self.radius = radius
    def get_circumference(self):
        return 2 * math.pi * self.radius
    def get_area(self):
        return math.pi * (self.radius ** 2)

circle = Circle(10)
print("μ›μ˜ λ‘˜λ ˆ:", circle.get_circumference())
print("μ›μ˜ 넓이:", circle.get_area())
PreviousString formatNextmodule

Last updated 4 years ago

Was this helpful?