python class 정리 🐍

JudyΒ·2023λ…„ 6μ›” 21일
0

Python 🐍

λͺ©λ‘ 보기
2/3

Why?

OOP (Object Oriented Programming)

  • For Object Oriented Programming
  • It helps to structure and modularize your code to improve maintainability and make your code more reusable

How?

Instance & Method

  • Data : Instance
  • Function : Method

Create instance & call method

class Person:
    # Initialize instance(object)
    def __init__(self):
    	# Create attribute
        # Assign value to 'self.attribute' in '__init__' method 
        self.hello = 'Hello'
 
    def greeting(self):
        # Using attributes inside a class
        print(self.hello)
 
# Instanse = class()
# Create an instance to use the class
james = Person()
# Call method
james.greeting()    # "Hello"

Get parameter when creating instance

class Person:
    def __init__(self, name, age, address):
        self.hello = 'μ•ˆλ…•ν•˜μ„Έμš”.'
        self.name = name
        self.age = age
        self.address = address
 
    def greeting(self):
        print('{0} μ €λŠ” {1}μž…λ‹ˆλ‹€.'.format(self.hello, self.name))
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™')
maria.greeting()    # μ•ˆλ…•ν•˜μ„Έμš”. μ €λŠ” λ§ˆλ¦¬μ•„μž…λ‹ˆλ‹€.
 
print('이름:', maria.name)       # λ§ˆλ¦¬μ•„
print('λ‚˜μ΄:', maria.age)        # 20
print('μ£Όμ†Œ:', maria.address)    # μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™

Inheritance

Get inheritance

class Person:

    def __init__(self):
        self.name = 'judy.'

    def greeting(self):
        print(f'μ•ˆλ…•ν•˜μ„Έμš”. μ €λŠ” {self.name} μž…λ‹ˆλ‹€.')
 
# Inherit class 'Person'
class Student(Person):
    def study(self):
        print('κ³΅λΆ€ν•˜κΈ°')
 
james = Student()
james.greeting()    # μ•ˆλ…•ν•˜μ„Έμš”.
james.study()       # κ³΅λΆ€ν•˜κΈ°

Private attribute & method

  • Private attribute that is not accessible from outside the class and can only be used inside the class.
  • Used when it is an important value and should not be changed outside
class Person:
    def __init__(self, name, age, address, wallet):
        self.name = name
        self.age = age
        self.address = address
        self.__wallet = wallet    # '__value' : private
 
    def pay(self, amount):
        self.__wallet -= amount   # Only can access to private attribute with method in class
        print('이제 {0}원 λ‚¨μ•˜λ„€μš”.'.format(self.__wallet))

    def __greeting(self):   # '__function' : private
        print('Hello')
 
    def hello(self):
        self.__greeting()    # Can call private method in class
 
 
maria = Person('λ§ˆλ¦¬μ•„', 20, 'μ„œμšΈμ‹œ μ„œμ΄ˆκ΅¬ λ°˜ν¬λ™', 10000)

# Error : AttributeError: 'Person' object has no attribute '__wallet'
# Accessing private properties/methods is not allowed from outside the class
maria.__wallet -= 10000
maria.__greeting()

# Accessible using methods created within the class
maria.pay(3000)

Reference

클래슀 μ‚¬μš©ν•˜κΈ°
https://dojang.io/mod/page/view.php?id=2372
클래슀 속성, λ©”μ†Œλ“œ
https://dojang.io/mod/page/view.php?id=2378
클래슀 상속 μ‚¬μš©ν•˜κΈ°
https://dojang.io/mod/page/view.php?id=2384

profile
NLP Researcher

0개의 λŒ“κΈ€