...
 
Commits (2)
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
......@@ -21,3 +21,12 @@ class Car:
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}.")
......@@ -12,8 +12,16 @@ def _get(self, attr):
def _set(self, attr, value):
self[attr] = value
def _get_kind(self):
return self['kind']
def _set_kind(self, value):
self['kind'] = value
def eat(self):
set(self, 'fresh', False)
def throw(self, at_who):
print(f"Throwing a {get(self, 'kind')} at {at_who}.")
......@@ -28,10 +28,22 @@ def new(cls, *args, **kw):
return self
def get(self, attr):
cls = self['class']
get_m = cls.__dict__.get('_get_' + attr)
if get_m:
return get_m(self['data'])
else:
get_m = method(self, 'get')
return get_m(self['data'], attr)
def set(self, attr, value):
cls = self['class']
set_m = cls.__dict__.get('_set_' + attr)
if set_m:
return set_m(self['data'], value)
else:
set_m = method(self, 'set')
return set_m(self['data'], attr, value)
......
......@@ -4,6 +4,7 @@ from pprint import pprint
import Car
import Fruit
import Apple
def test_new():
a = new(Fruit, "apple", "red")
......@@ -42,16 +43,17 @@ def test_obj_methods():
c.drive("Zed", "Texas")
def test_inheritance():
a = new(Apple, 'red')
assert get(a, 'fresh') == True
assert get(a, 'kind') == 'apple'
assert get(a, 'color') == 'red'
set(a, 'color', 'green')
assert get(a, 'color') == 'green'
Apple.eat(a)
assert get(a, 'fresh') == False
Apple.throw(a, 'Zed')