Commit 2a2c566b authored by Zed's avatar Zed

Results of Seminar 2.

parent a043fec8
from oop import *
def _init(self, kind, color):
self['kind'] = kind
self['color'] = color
def new(kind, color):
return {'kind': kind, 'color': color}
def drive(self, who, where):
kind = get(self, 'kind')
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}")
from oop import *
from pprint import pprint
def new(kind, color):
return {'kind': kind, 'color': color}
def _init(self, kind, color):
self['kind'] = kind
self['color'] = color
self['fresh'] = True
def get(obj, attr):
return obj[attr]
def _get(self, attr):
return self[attr]
def set(obj, attr, value):
obj[attr] = value
def _set(self, attr, value):
self[attr] = value
def eat(self):
set(self, 'fresh', False)
def throw(self, at_who):
print(f"Throwing a {get(self, 'kind')} at {at_who}.")
......@@ -5,36 +5,35 @@ def load_class(class_name):
assert cls, "Didn't get module '%s'" % class_name
return cls
def method(obj, name):
return obj['methods'][name]
def method(self, name):
return self['methods'][name]
def Class_get(obj, attr):
return obj[attr]
def Class_get(self, attr):
return self[attr]
def Class_set(obj, attr, value):
obj[attr] = value
def Class_set(self, attr, value):
self[attr] = value
def new(class_name, *args, **kw):
cls = load_class(class_name)
def new(cls, *args, **kw):
data = {}
cls._init(data, *args, **kw)
data = cls.new(*args, **kw)
methods = {'get': cls.__dict__.get('_get', Class_get),
'set': cls.__dict__.get('_set', Class_set),}
methods = {'get': cls.__dict__.get('get', Class_get),
'set': cls.__dict__.get('set', Class_set),}
obj = {'data': data,
'name': class_name,
self = {'data': data,
'class': cls,
'methods': methods}
return obj
return self
def get(obj, attr):
get_m = method(obj, 'get')
return get_m(obj['data'], attr)
def get(self, attr):
get_m = method(self, 'get')
return get_m(self['data'], attr)
def set(obj, attr, value):
set_m = method(obj, 'set')
return set_m(obj['data'], attr, value)
def set(self, attr, value):
set_m = method(self, 'set')
return set_m(self['data'], attr, value)
def is_a(obj, class_name):
return obj['name'] == class_name
def is_a(self, cls):
return self['class'] == cls
from oop import *
from pprint import pprint
import Car
import Fruit
def test_new():
a = new("Fruit", "apple", "red")
assert is_a(a, "Fruit")
a = new(Fruit, "apple", "red")
assert is_a(a, Fruit)
assert get(a, 'color') == "red"
assert get(a, 'kind') == "apple"
......@@ -14,12 +17,41 @@ def test_new():
assert get(a, 'kind') == "orange"
def test_new_class_no_getset():
b = new("Car", "buick", "green")
assert is_a(b, "Car")
b = new(Car, "buick", "green")
assert is_a(b, Car)
assert get(b, 'color') == 'green'
assert get(b, 'kind') == 'buick'
set(b, 'color', 'red')
assert get(b, 'color') == 'red'
call(b, 'drive', 'Texas')
def test_obj_methods():
b = new(Fruit, "apple", "red")
assert get(b, 'fresh') == True
Fruit.eat(b)
assert get(b, 'fresh') == False
Fruit.throw(b, 'Zed')
c = new(Car, "buick", "blue")
Car.drive(c, "Zed", "Texas")
c = Car.Car("buick", "blue")
c.drive("Zed", "Texas")
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