Commit e748d43e authored by Zed's avatar Zed

S4 code.

parent 814dd480
from oop import *
import Fruit
IS_A = Fruit
def _init(self, color):
Fruit._init(self, 'apple', color)
_set = Fruit._set
_get = Fruit._get
_get_kind = Fruit._get_kind
_set_kind = Fruit._set_kind
eat = Fruit.eat
throw = Fruit.throw
inherits(Fruit, _init)
from oop import *
import Car
def _init(self, color):
Car._init(self, 'buick', color)
def drive(self):
print('Zed goes to Chicago always.')
inherits(Car, _init)
......@@ -9,24 +9,3 @@ def drive(self, who, where):
color = get(self, 'color')
print(f"{who} will drive a {color} {kind} to {where}")
class Car:
def __init__(self, kind, color):
self.kind = kind
self.color = color
def drive(self, who, where):
kind = self.kind
color = self.color
print(f"{who} will drive a {color} {kind} to {where}")
class Buick(Car):
def __init__(self, color):
super().__init__('buick', color)
def park(self, where):
print(f"You are parking your buick in {where}.")
from oop import *
import Apple
def _init(self):
Apple._init(self, 'green')
inherits(Apple, _init)
from importlib import import_module
import inspect
def load_class(class_name):
cls = import_module(class_name)
......@@ -49,3 +50,16 @@ def set(self, attr, value):
def is_a(self, cls):
return self['class'] == cls
def inherits(parent, _init):
child = inspect.getmodule(_init)
p_dict = parent.__dict__
p_keys = p_dict.keys()
p_members = ((k, p_dict[k]) for k in p_keys if inspect.isfunction(p_dict[k]))
for key, func in p_members:
if key not in child.__dict__:
child.__dict__[key] = func
child.IS_A = parent
......@@ -5,6 +5,8 @@ from pprint import pprint
import Car
import Fruit
import Apple
import Buick
import Grannysmith
def test_new():
a = new(Fruit, "apple", "red")
......@@ -39,10 +41,6 @@ def test_obj_methods():
c = new(Car, "buick", "blue")
Car.drive(c, "Zed", "Texas")
c = Car.Car("buick", "blue")
c.drive("Zed", "Texas")
def test_inheritance():
a = new(Apple, 'red')
assert get(a, 'fresh') == True
......@@ -57,3 +55,13 @@ def test_inheritance():
Apple.throw(a, 'Zed')
gs = new(Grannysmith)
Grannysmith.throw(gs, 'Zed')
# test that the base class and work with subclasses
Fruit.throw(gs, 'Zed')
Fruit.throw(a, 'Zed')
mycar = new(Buick, 'green')
Buick.drive(mycar)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment