class ν΄λμ€μ΄λ¦:
def λ©μλ(self):
μ½λ
class Person:
def greeting(self):
print('Hello')
# μΈμ€ν΄μ€ = ν΄λμ€()
james = Person()
# μΈμ€ν΄μ€.λ©μλ()
james.greeting()
Hello
a = int(10)
a
10
b = list(range(10))
b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
c = dict(x=10, y=20)
c
{'x': 10, 'y': 20}
b = list(range(10))
b.append(20)
b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
# type(κ°μ²΄)
a = 10
b = [0, 1, 2]
type(b)
list
c = {'x':10, 'y':20}
type(c)
dict
maria = Person()
type(maria)
__main__.Person
# 리μ€νΈ λ³μ a, b λ κ°μ²΄
# a, bλ list ν΄λμ€μ μΈμ€ν΄μ€
# μ¬λμ΄λ νκ΅ μ¬λμ΄λ μΈκ±°μ§....
a = list(range(10))
b = list(range(20))
λΉν΄λμ€ λ§λ€κΈ°
class Person:
pass
λ©μλ μμμ λ©μλ νΈμΆνκΈ°
class Person:
def greeting(self):
print('Hello')
def hello(self):
self.greeting() # self.λ©μλ() νμμΌλ‘ ν΄λμ€ μμ λ©μλλ₯Ό νΈμΆ
james = Person()
james.hello() # Hello
Hello
νΉμ ν΄λμ€μ μΈμ€ν΄μ€μΈμ§ νμΈνκΈ°
class Person:
pass
james = Person()
isinstance(james, Person)
True
# isinstance λ κ°μ²΄μ μλ£ν νλ¨ μ μ¬μ© (νΉμ 쑰건μΌλ‘ μ¬μ©)
def factorial(n):
if not isinstance(n, int) or n < 0: # nμ΄ μ μκ° μλκ±°λ μμμ΄λ©΄ ν¨μλ₯Ό λλ
return None
if n == 1:
return 1
return n * factorial(n - 1)
class Person:
def __init__(self, name):
self.name = name
self.name
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/tmp/ipykernel_1427/2604046875.py in <module>
2 def __init__(self, name):
3 self.name = name
----> 4 self.name()
NameError: name 'self' is not defined
# μμ±μ λ§λ€λλ __int__ λ©μλ μμμ self.μμ±μ κ°μ ν λΉ
class ν΄λμ€μ΄λ¦:
def __init__(self):
self.μμ± = κ°
# Person ν΄λμ€μ __init__ λ©μλμμ self.helloμ 'μλ
νμΈμ.' μΈμ¬λ§
class Person:
def __init__(self):
self.hello = 'μλ
νμΈμ.'
# greeting λ©μλλ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€. greeting λ©μλμμλ printλ‘ self.helloλ₯Ό μΆλ ₯
def greeting(self):
print(self.hello)
# Person ν΄λμ€λ‘ μΈμ€ν΄μ€λ₯Ό λ§λ¦
james = Person()
# greeting λ©μλλ₯Ό νΈμΆ
james.greeting() # self.helloμ μ μ₯λ 'μλ
νμΈμ.' κ° μΆλ ₯
μλ
νμΈμ.
class ν΄λμ€μ΄λ¦:
def __init__(self, 맀κ°λ³μ1, 맀κ°λ³μ2):
self.μμ±1 = 맀κ°λ³μ1
self.μμ±2 = 맀κ°λ³μ2
class Person:
def __init__(self, name, age, address): # self λ€μμ name, age, addressλ₯Ό μ§μ
self.hello = 'μλ
νμΈμ.'
self.name = name
self.age = age
self.address = address
# λ©μλ μμμλ self.name = nameμ²λΌ 맀κ°λ³μλ₯Ό κ·Έλλ‘ selfμ λ£μ΄μ μμ±μΌλ‘
def greeting(self): # greeting λ©μλλ μΈμ¬λ₯Ό νκ³ μ΄λ¦μ μΆλ ₯
print('{0} μ λ {1}μ
λλ€.'.format(self.hello, self.name))
# name μμ±μ μ κ·Όν λλ self.nameμ²λΌ μ¬μ©
maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ') # μΈμ€ν΄μ€ = ν΄λμ€()
maria.greeting() # μλ
νμΈμ. μ λ λ§λ¦¬μμ
λλ€. //maria μΈμ€ν΄μ€μ greeting λ©μλλ₯Ό νΈμΆ
# ν΄λμ€ λ°κΉ₯μμ μμ±μ μ κ·Όν λλ μΈμ€ν΄μ€.μμ± νμμΌλ‘ μ κ·Ό
print('μ΄λ¦:', maria.name) # λ§λ¦¬μ
print('λμ΄:', maria.age) # 20
print('μ£Όμ:', maria.address) # μμΈμ μμ΄κ΅¬ λ°ν¬λ
μλ
νμΈμ. μ λ λ§λ¦¬μμ
λλ€.
μ΄λ¦: λ§λ¦¬μ
λμ΄: 20
μ£Όμ: μμΈμ μμ΄κ΅¬ λ°ν¬λ
# ν΄λμ€μ μμΉ μΈμ
# **args -> args[0]
class Person:
def __init__(self, *args):
self.name = args[0]
self.age = args[1]
self.address = args[2]
maria = Person(*['λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ'])
# maria μΈμ€ν΄μ€ Person ν΄λμ€μ name, age, address μμ±
# ν΄λμ€μ ν€μλ μΈμμ λμ
λ리 μΈν¨νΉ
# **kwargs kwargs['name']
class Person:
def __init__(self, **kwargs): # ν€μλ μΈμ
self.name = kwargs['name']
self.age = kwargs['age']
self.address = kwargs['address']
maria1 = Person(name='λ§λ¦¬μ', age=20, address='μμΈμ μμ΄κ΅¬ λ°ν¬λ')
maria2 = Person(**{'name': 'λ§λ¦¬μ', 'age': 20, 'address': 'μμΈμ μμ΄κ΅¬ λ°ν¬λ'})
# μΈμ€ν΄μ€λ₯Ό μμ±ν λ€μ μμ± μΆκ°
#
# Person ν΄λμ€λ λΉ ν΄λμ€μ΄μ§λ§ μΈμ€ν΄μ€λ₯Ό λ§λ λ€ name μμ±μ μΆκ°
class Person:
pass
maria = Person() # μΈμ€ν΄μ€ μμ±
maria.name = 'λ§λ¦¬μ' # μΈμ€ν΄μ€λ₯Ό λ§λ λ€ μμ± μΆκ°
maria.name
'λ§λ¦¬μ'
# ν΄λΉ μΈμ€ν΄μ€λ§ μμ± λ€λ₯Έ μΈμ€ν΄μ€λ₯Ό λ§λ€μμ λ μΆκ°ν μμ±μ μμ± μλ¨
james = Person() # james μΈμ€ν΄μ€ μμ±
james.name # maria μΈμ€ν΄μ€μλ§ name μμ±μ μΆκ°νμΌλ―λ‘ james μΈμ€ν΄μ€μλ name μμ±μ΄ μμ
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1427/1051155537.py in <module>
1 # ν΄λΉ μΈμ€ν΄μ€λ§ μμ±
2 james = Person() # james μΈμ€ν΄μ€ μμ±
----> 3 james.name # maria μΈμ€ν΄μ€μλ§ name μμ±μ μΆκ°νμΌλ―λ‘ james μΈμ€ν΄μ€μλ name μμ±μ΄ μμ
AttributeError: 'Person' object has no attribute 'name'
#
#
class Person:
def greeting(self):
self.hello = 'μλ
νμΈμ' # greeting λ©μλμμ hello μμ± μΆκ°
maria = Person()
maria.hello # μμ§ hello μμ±μ΄ μμ
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1427/2501446103.py in <module>
6
7 maria = Person()
----> 8 maria.hello # μμ§ hello μμ±μ΄ μμ
AttributeError: 'Person' object has no attribute 'hello'
maria.greeting() # greeting λ©μλλ₯Ό νΈμΆν΄μΌ
maria.hello # hello μμ±μ΄ μμ±λ¨
'μλ
νμΈμ'
# νΉμ μμ±λ§ νμ©νκ³ λ€λ₯Έ μμ±μ μ ν
# ν΄λμ€μμ __slots__μ νμ©ν μμ± μ΄λ¦μ 리μ€νΈλ‘
# __slots__ = ['μμ±μ΄λ¦1, 'μμ±μ΄λ¦2']
class Person:
__slots__ = ['name', 'age'] # name, ageλ§ νμ©(λ€λ₯Έ μμ±μ μμ± μ ν)
maria = Person()
maria.name = 'λ§λ¦¬μ' # νμ©λ μμ±
maria.age = 20 # νμ©λ μμ±
maria.address = 'μμΈμ μμ΄κ΅¬ λ°ν¬λ' # νμ©λμ§ μμ μμ±μ μΆκ°ν λ μλ¬κ° λ°μν¨
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1427/1073807025.py in <module>
5 maria.name = 'λ§λ¦¬μ' # νμ©λ μμ±
6 maria.age = 20 # νμ©λ μμ±
----> 7 maria.address = 'μμΈμ μμ΄κ΅¬ λ°ν¬λ' # νμ©λμ§ μμ μμ±μ μΆκ°ν λ μλ¬κ° λ°μν¨
AttributeError: 'Person' object has no attribute 'address'
Person ν΄λμ€μλ hello, name, age, address μμ±
class Person:
def __init__(self, name, age, address):
self.hello = 'μλ
νμΈμ.'
self.name = name
self.age = age
self.address = address
# μ΄ μμ±λ€μ λ©μλμμ selfλ‘ μ κ·Όν μ μκ³ , μΈμ€ν΄μ€.μμ± νμμΌλ‘ ν΄λμ€ λ°κΉ₯μμλ μ κ·Ό
maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ')
maria.name
'λ§λ¦¬μ'
# λΉκ³΅κ° μμ±μ __μμ±κ³Ό κ°μ΄ μ΄λ¦μ΄ __(λ°μ€ λ κ°)λ‘ μμ
class ν΄λμ€μ΄λ¦:
def __init__(self, 맀κ°λ³μ)
self.__μμ± = κ°
File "/tmp/ipykernel_1427/1631572235.py", line 3
def __init__(self, 맀κ°λ³μ)
^
SyntaxError: invalid syntax
class Person:
def __init__(self, name, age, address, wallet):
self.name = name
self.age = age
self.address = address
self.__wallet = wallet # λ³μ μμ __λ₯Ό λΆμ¬μ λΉκ³΅κ° μμ±μΌλ‘ λ§λ¦
maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ', 10000)
maria.__wallet -= 10000 # ν΄λμ€ λ°κΉ₯μμ λΉκ³΅κ° μμ±μ μ κ·Όνλ©΄ μλ¬κ° λ°μν¨
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1427/3017173123.py in <module>
7
8 maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ', 10000)
----> 9 maria.__wallet -= 10000 # ν΄λμ€ λ°κΉ₯μμ λΉκ³΅κ° μμ±μ μ κ·Όνλ©΄ μλ¬κ° λ°μν¨
AttributeError: 'Person' object has no attribute '__wallet'
# λΉκ³΅κ° μμ±μ ν΄λμ€ μμ λ©μλμμλ§ μ κ·Ό
class Person:
def __init__(self, name, age, address, wallet):
self.name = name
self.age = age
self.address = address
self.__wallet = wallet # λ³μ μμ __λ₯Ό λΆμ¬μ λΉκ³΅κ° μμ±μΌλ‘ λ§λ¦
def pay(self, amount):
self.__wallet -= amount # λΉκ³΅κ° μμ±μ ν΄λμ€ μμ λ©μλμμλ§ μ κ·Όν μ μμ
print('μ΄μ {0}μ λ¨μλ€μ.'.format(self.__wallet))
maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ', 10000)γ
γ
maria.pay(3000)
μ΄μ 7000μ λ¨μλ€μ.
# μλ λΆλΆμ²λΌ ν΄λμ€ μμ±μ λ°κΉ₯μΌλ‘ λλ¬λ΄μ§ μμ λ μ¬μ© κ°λ₯.
def pay(self, amount):
if amount > self.__wallet: # μ¬μ©νλ €κ³ νλ κΈμ‘λ³΄λ€ μ§κ°μ λ λμ΄ μ μ λ
print('λμ΄ λͺ¨μλΌλ€...')
return
self.__wallet -= amount
File "/tmp/ipykernel_1427/2524260839.py", line 2
def pay(self, amount):
^
IndentationError: unexpected indent
class Person:
def __init__(self, name, age, address, wallet):
self.name = name
self.age = age
self.address = address
self.__wallet = wallet # λ³μ μμ __λ₯Ό λΆμ¬μ λΉκ³΅κ° μμ±μΌλ‘ λ§λ¦
def pay(self, amount):
if amount > self.__wallet: # μ¬μ©νλ €κ³ νλ κΈμ‘λ³΄λ€ μ§κ°μ λ λμ΄ μ μ λ
print('λμ΄ λͺ¨μλΌλ€...')
return
self.__wallet -= amount
maria = Person('λ§λ¦¬μ', 20, 'μμΈμ μμ΄κ΅¬ λ°ν¬λ', 10000)
maria.pay(100000) # μ§λΆν κ°κ²©
λμ΄ λͺ¨μλΌλ€...
ν΄λμ€ λ°κΉ₯μμ μ κ·Όν μ μλ μμ±μ κ³΅κ° μμ±(public attribute)μ΄λΌ λΆλ₯΄κ³ ,
ν΄λμ€ μμμλ§ μ κ·Όν μ μλ μμ±μ λΉκ³΅κ° μμ±(private attribute)μ΄λΌ λΆλ¦ λλ€.
# μμ±λΏλ§ μλλΌ λ©μλλ μ΄λ¦μ΄ __(λ°μ€ λ κ°)λ‘ μμνλ©΄ ν΄λμ€ μμμλ§ νΈμΆν μ μλ λΉκ³΅κ° λ©μλκ° λ©
class Person:
def __greeting(self):
print('Hello')
def hello(self):
self.__greeting() # ν΄λμ€ μμμλ λΉκ³΅κ° λ©μλλ₯Ό νΈμΆν μ μμ
james = Person()
james.__greeting() # μλ¬: ν΄λμ€ λ°κΉ₯μμλ λΉκ³΅κ° λ©μλλ₯Ό νΈμΆν μ μμ
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_1427/199009468.py in <module>
7
8 james = Person()
----> 9 james.__greeting() # μλ¬: ν΄λμ€ λ°κΉ₯μμλ λΉκ³΅κ° λ©μλλ₯Ό νΈμΆν μ μμ
AttributeError: 'Person' object has no attribute '__greeting'
...
x = Knight(health=542.4, mana=210.3, armor=38) # μ λμ΄ ν΄λμ€λ€
print(x.health, x.mana, x.armor)
x.slash()
class Knight:
def __init__(self, health, mana, armor):
self.health = health
self.mana = mana
self.armor = armor
def slash(self):
print('λ² κΈ°')
x = Knight(health=542.4, mana=210.3, armor=38)
print(x.health, x.mana, x.armor)
x.slash()
542.4 210.3 38
λ² κΈ°
class ν΄λμ€μ΄λ¦: # Classκ° ν¬ν¨λμ΄ μλ Global frame
def λ©μλ(self(맀κ°λ³μλ₯Ό λκ³ κ°λ κΈ°μ°¨), 맀κ°λ³μ1, 맀κ°λ³μ2, 맀κ°λ³μ3): # λ©μλμ 맀κ°λ³μλ₯Ό ν©μ³μ function
self.μμ±1 = 맀κ°λ³μ1
self.μμ±2 = 맀κ°λ³μ2
self.μμ±3 = 맀κ°λ³μ3
#μ
λ ₯λ°μλ?
맀κ°λ³μ1, 맀κ°λ³μ2, 맀κ°λ³μ3 = (맀κ°λ³μ κ°)
μΈμ€ν΄μ€ = ν΄λμ€(맀κ°λ³μ1=κ°, 맀κ°λ³μ2=κ°, 맀κ°λ³μ3=κ°)
print(μΈμ€ν΄μ€.맀κ°λ³μ1, μΈμ€ν΄μ€.맀κ°λ³μ2, μΈμ€ν΄μ€.맀κ°λ³μ3)
μΈμ€ν΄μ€.λ©μλ()
νμ€ μ λ ₯μΌλ‘ κ²μ μΊλ¦ν° λ₯λ ₯μΉ(체λ ₯, λ§λ, AP)κ° μ λ ₯λ©λλ€.
λ€μ μμ€ μ½λμμ μ λ(Annie) ν΄λμ€λ₯Ό μμ±νμ¬
ν°λ²(tibbers) μ€ν¬μ νΌν΄λμ΄ μΆλ ₯λκ² λ§λμΈμ.
ν°λ²μ νΌν΄λμ AP * 0.65 + 400μ΄λ©° AP(Ability Power, μ£Όλ¬Έλ ₯)λ λ§λ² λ₯λ ₯μΉλ₯Ό λ»ν©λλ€.
class Annie:
def __init__(self, health, mana, ability_power):
self.health = health
self.mana = mana
self.ability_power = ability_power
def tibbers(self):
print('ν°λ²: νΌν΄λ ' ,ability_power*0.65+400)
health, mana, ability_power = map(float, input().split())
x = Annie(health=health, mana=mana, ability_power=ability_power)
x.tibbers()
511.68 334 298
ν°λ²: νΌν΄λ 593.7
class Annie:
def __init__(self, health, mana, ability_power):
self.health = health
self.mana = mana
self.ability_power = ability_power
def tibbers(ability_powers):
print('tibber:', 'damage', ability_power*0.65+400)
health, mana, ability_power = map(float, input().split())
x = Annie(health=health, mana=mana, ability_power=ability_power)
x.tibbers()
511.68 334 298
tibber: damage 593.7
class ν΄λμ€μ΄λ¦: # Classκ° ν¬ν¨λμ΄ μλ Global frame
def λ©μλ(self(맀κ°λ³μλ₯Ό λκ³ κ°λ κΈ°μ°¨), 맀κ°λ³μ1, 맀κ°λ³μ2, 맀κ°λ³μ3): # λ©μλμ 맀κ°λ³μλ₯Ό ν©μ³μ function
self.μμ±1 = 맀κ°λ³μ1
self.μμ±2 = 맀κ°λ³μ2
self.μμ±3 = 맀κ°λ³μ3
#μ
λ ₯λ°μλ?
맀κ°λ³μ1, 맀κ°λ³μ2, 맀κ°λ³μ3 = (맀κ°λ³μ κ°)
μΈμ€ν΄μ€ = ν΄λμ€(맀κ°λ³μ1=κ°, 맀κ°λ³μ2=κ°, 맀κ°λ³μ3=κ°)
print(μΈμ€ν΄μ€.맀κ°λ³μ1, μΈμ€ν΄μ€.맀κ°λ³μ2, μΈμ€ν΄μ€.맀κ°λ³μ3)
μΈμ€ν΄μ€.λ©μλ()