Review_02_25_class

DODO·2026년 2월 26일

Review_for class

목록 보기
8/20

Inheritance

organized class structure to avoid repeating the same code
when starting a child classs, put the parent class name in brackets
super().init()
overriding a parent class method by defining a function with the same name.


Method Overriding
Redefining a parent's method in a child class to change its behavior


-ex-
class MedicalStaff:
 def init(self, name, department):
   self.name = name
   self.department = departmentdef
  def introduce(self):
    return f"hello. I'm {self.department} {self.name}"


class Doctor(MedicalStaff):
  def init(self, specialty):
    super. init(name,department)
    self.specialty = specialty
   def introduce(self):
     return f"hello. I'm {self.specailty}{self.name}"
     • only change from department to specailty

File I/O is required

open() - Open the file, check the mode and decide what do we do
basic: file = open('file name.txt', 'mode', encoding='utf-8') and file.close()
   c'file close' is very important !!!
w= write , r= read, a = add, r+ = read and write, utf-8 = korean
Using with is safer than not using it.!
- ex -
😒
patient_file = open('patient_info.txt', 'w', encoding='utf-8')
patient_file.write('patient info')
patient_file.close()
😊
with open('patient_info.txt','w',encoding='utf-8') as file:
file.write('patient info')

exception handling

try - except
- ex -
try:
 -> Error-prone code
except:
 ->Catch every error
 print(' A error occurred')
finally block: this code always runs without any errors
 except ZeroDivisionError:# key = 0
 print('height is bigger than zero')
 except ValueError:# when enter str, instead of int
 print('only enter numbers')
 except Exception as e:# unexpected error
 print(f"an error occured: {e}")
 finally:
 print('ending the bmi calculate')
 print('record in the system log')


  more ~~## FileNotFoundError - no exist
PermissionError - reject permission
ValueError - value is wrong
IsADirectoryError - try opening folder instead of file
UnicodeDecodeError - read wrong encoding
OSError - error about OS
IOError - IN/OUT General error

my review file

0개의 댓글