global_var = 100 def func1(): print(global_var) def func2(): print(global_var) global_var = 200 print("func1を実行") func1() print("func2を実行") func2() class Car: def __init__(self, owner, color, model): self.owner = owner self.color = color self.model = model def open(self): print(f"{self.owner}さんの{self.color}の{self.model}のドアが開いた!") def run(self): print(f"{self.owner}さんの{self.color}の{self.model}が走った!") red_car = Car("田中", "赤", "乗用車") red_car.open() red_car.run() print("-"*50) white_car = Car("鈴木", "白", "トラック") white_car.open() white_car.run() class Machine: def __init__(self, owner, color, model): self.owner = owner self.color = color self.model = model def open(self): print(f"{self.owner}さんの{self.color}の{self.model}のドアが開いた!") def move(self): # 「走る」「飛ぶ」など様々な乗り物が動く機能を抽象化する print(f"{self.owner}さんの{self.color}の{self.model}が動いた!") class Car(Machine): def move(self): # Machineクラスのmoveを上書きして「走る」にする print(f"{self.owner}さんの{self.color}の{self.model}が走った!") class Airplane(Machine): def move(self): # Machineクラスのmoveを上書きして「飛ぶ」にする print(f"{self.owner}さんの{self.color}の{self.model}が飛んだ!") red_car = Car("田中", "赤", "乗用車") red_car.open() red_car.move() print("-"*50) blue_airplane = Airplane("佐藤", "青", "ヘリ") blue_airplane.open() blue_airplane.move() try: divval = 0 print(100/divval) except ZeroDivisionError: print("ZeroDivisionErrorが発生しました") try: idx = 10 mylist = ["Tokyo", "Osaka", "Nagoya"] print(mylist[idx]) except IndexError: print("IndexErrorが発生しました") try: with open("dummy", "r") as f: print(type(f)) except OSError as e: print("OSError: " + str(e)) import traceback try: #---------------------------------- # データ構造化のメイン処理を記述 #---------------------------------- # この例ではわざとFileNotFoundErrorを発生させる with open("dummy") as f: print(type(f)) except: print(traceback.format_exc()) exit(1)