Guidelines

객체 μ‚¬μ΄μ—μ„œ 값이 κ³΅μœ λ˜λŠ” 클래슀 λ³€μˆ˜

클래슀 λ³€μˆ˜(Class Variable)`λŠ” ν΄λž˜μŠ€μ— μ†ν•œ λ³€μˆ˜λ‘œ, ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  객체 μ‚¬μ΄μ—μ„œ 값이 κ³΅μœ λ©λ‹ˆλ‹€.

클래슀 λ³€μˆ˜λŠ” 클래슀 μ„ μ–Έ λ‚΄λΆ€μ—μ„œ μ •μ˜λ˜λ©°, 보톡 클래슀 μ •μ˜μ˜ μ΅œμƒλ‹¨μ— μœ„μΉ˜ν•©λ‹ˆλ‹€.

μƒμ„±μž __init__을 ν™œμš©ν•΄ μƒμ„±λœ κ°μ²΄λ³„λ‘œ 독립적인 값을 κ°–λŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ™€ 달리, 클래슀 λ³€μˆ˜λŠ” ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  객체가 κ³΅μœ ν•˜λŠ” 값을 κ°–μŠ΅λ‹ˆλ‹€.


클래슀 λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ

클래슀 λ³€μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
class MyClass: # 클래슀 λ³€μˆ˜ class_variable = "곡유 λ³€μˆ˜" def __init__(self, name): # μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ self.name = name # 클래슀 λ³€μˆ˜μ— μ ‘κ·Ό print(MyClass.class_variable) # 좜λ ₯: 곡유 λ³€μˆ˜ # μΈμŠ€ν„΄μŠ€ 생성 obj1 = MyClass("객체1") obj2 = MyClass("객체2") # 클래슀 λ³€μˆ˜λŠ” λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ—μ„œ 곡유 print(obj1.class_variable) # 좜λ ₯: 곡유 λ³€μˆ˜ print(obj2.class_variable) # 좜λ ₯: 곡유 λ³€μˆ˜ # 클래슀 λ³€μˆ˜λ₯Ό μˆ˜μ • MyClass.class_variable = "λ³€κ²½λœ κ°’" print(obj1.class_variable) # 좜λ ₯: λ³€κ²½λœ κ°’ print(obj2.class_variable) # 좜λ ₯: λ³€κ²½λœ κ°’ # μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λŠ” κ°œλ³„μ μœΌλ‘œ μœ μ§€ print(obj1.name) # 좜λ ₯: 객체1 print(obj2.name) # 좜λ ₯: 객체2

이 μ½”λ“œμ—μ„œ class_variable은 MyClass ν΄λž˜μŠ€μ— μ†ν•œ 클래슀 λ³€μˆ˜λ‘œ, MyClass ν΄λž˜μŠ€μ— μ˜ν•΄ μƒμ„±λœ λͺ¨λ“  객체 μ‚¬μ΄μ—μ„œ 값이 κ³΅μœ λ©λ‹ˆλ‹€.

obj1κ³Ό obj2 μΈμŠ€ν„΄μŠ€λŠ” 각각 name μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό 가지고 있으며, 이 λ³€μˆ˜λŠ” κ°μ²΄λ³„λ‘œ 독립적인 값을 μœ μ§€ν•©λ‹ˆλ‹€.

클래슀 λ³€μˆ˜λŠ” 클래슀 이름을 톡해 μ ‘κ·Όν•  수 있으며, 클래슀 λ³€μˆ˜μ˜ 값을 μˆ˜μ •ν•˜λ©΄ λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— 영ν–₯을 μ€λ‹ˆλ‹€.

Mission
0 / 1

클래슀 λ³€μˆ˜μ— λŒ€ν•œ μ„€λͺ… 쀑 μ˜³μ€ 것은 λ¬΄μ—‡μΌκΉŒμš”?

클래슀 λ³€μˆ˜λŠ” 각 κ°μ²΄λ§ˆλ‹€ λ³„λ„λ‘œ μ €μž₯λœλ‹€.

클래슀 λ³€μˆ˜λŠ” μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ™€ λ™μΌν•œ λ°©μ‹μœΌλ‘œ μž‘λ™ν•œλ‹€.

클래슀 λ³€μˆ˜λŠ” 클래슀의 λͺ¨λ“  μΈμŠ€ν„΄μŠ€ 간에 κ³΅μœ λœλ‹€.

클래슀 λ³€μˆ˜λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•΄μ•Όλ§Œ μ ‘κ·Όν•  수 μžˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result