Professional Documents
Culture Documents
SIC - C - P - DS - Chapter 3 - Unit21 - L
SIC - C - P - DS - Chapter 3 - Unit21 - L
('The account balance is {} won. This is smaller than the requested amount of withdrawal' \
'{} won.'.format(self.balance, amount))
UNIT 21
Start
[1] Start
Label
tab
text field
button
attribute attribute
object1 object2
'tiger'.upper()
animals
['lion', 'tiger', 'cat', 'dog']
others
id() = 1764472659904
UNIT 21
attributes ‘nabi’
name ‘black’
color
actions
meow ‘nero’
run ‘white’
walk
actions
meow
run
walk
Line 4, 5
• Parentheses are needed to generate a Cat instance.
• When runing print(nabi), the id value of the object is printed in hexadecimals.
UNIT 21
TIP
UNIT 21
print('meow meow~~~')
print('meow meow~~~')
# A string expression format of the Cat class A special method that defines string
expression format of the Cat class
Cat(name=nabi, color=black)
Cat(name=nero, color=white)
UNIT 21
class Cat:
def __init__(self, name, color):
…
nabi ==Cat('nabi',
Cat(‘나비’, ’검정색’)
'black')
mimi ==Cat('mimi',
Cat('미미', 'brown')
'갈색')
UNIT 21
self.name = name
self.color = color
self is interpreted as such for each instance.
Line 4, 5
• Nabi, Nero, Mimi are instances of the Cat class. Therefore, you can call meow, which is a method of the Cat class with
the . (dot) operator.
UNIT 21
# instance variable
# instance variable
# instance variable
# instance variable
# instance variable
# instance variable Circle class
instance variables
UNIT 21
class Circle:
PI = 3.1415 ## class variable
class variable
def __init__(self, name, radius):
self.__name = name # instance variable
self.__radius = radius # instance variable
PI Class
variable
# class variable
# instance variable
# instance variable
# Computes the area by using Circle’s class variable PI
child class
sub class
derived class
Rather detailed features
UNIT 21
# parent class A
PI = 3.13
# Generates instance a of class A
# Generates instance a of class B
Condition for
Execution woof woof
Time
Write the entire code and the expected output results in the note.
UNIT 21
Condition
for Execution Bingo : woof woof
Time
Write the entire code and the expected output results in the note.
UNIT 21
# limits random external access on self.__age and prevents age from being negative
2911697307008
a 765323962
‘ABC’
b
UNIT 21
Line 12
• Prints the sum of two vectors by using the add method named v1.add(v2).
• Using operators such as +, - will be more convenient that the add method.
UNIT 21
Line 5-6
• Defines the special method __add__ to return the sum of two vectors
UNIT 21
Line 14-15
•• 특수
Now 메소드 use v1 + v2 정의하여
you can__add__를 to call __두 벡터의 합을 반환하도록 구현함
add__
• The inner behavior of v1 + v2 is identical that of calling the method v1.__add(v2)
UNIT 21
+x __pos__(self) Gives x.
n 100
id: 140719510270864
UNIT 21
Line 3-4
• Since m and n both refer the number 100, the ids of the two objects are identical.
UNIT 21
200
n 100 n id: 140719510274064
100
The integer object 100 is a
n 100 n garbage object that is not used.
101
UNIT 21
부모 클래스
parent class
attribute : name
Person method: get_name()
Manager Employee
child class
UNIT 21
class Person:
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
class Manager(Person):
...
class Employee(Person):
...
UNIT 21
Person
- name
get_name()
Manager Employee
- position - StaffID
info() info()
Output example
Enter the student’s name : David Doe
Enter the student’s ID : 20213093
Enter the student’s English quiz score : 90
Enter the student’s mathematics quiz score : 95
Enter the student’s science quiz score : 100
Name : David Doe, ID : 20213093
English quiz score : 90, Mathematics quiz score : 95
Science quiz score : 100,
Total : 285, Average : 95.0