Commit 56ffbe2d authored by Zed's avatar Zed

First Seminar of OOP school.

parents
__pycache__
.cache
.*.sw*
def new(kind, color):
return {'kind': kind, 'color': color}
def new(kind, color):
return {'kind': kind, 'color': color}
def get(obj, attr):
return obj[attr]
def set(obj, attr, value):
obj[attr] = value
Questions:
* All the features of OOP, OR all the features people use?
* Do we do a python style OOP, or do we do a message passing system.
* Do we need efficiency?
Answers for First Attempt:
* Mostly the features people use.
- No MRO
* Python style OOP.
- No message passing.
* Not worry about efficiency.
Features:
* Functions on data.
* Subclass so a missing function will look at a parent class.
* Type information.
* Define a class, and then make objects from it.
* Encapsulation
from importlib import import_module
def load_class(class_name):
cls = import_module(class_name)
assert cls, "Didn't get module '%s'" % class_name
return cls
def method(obj, name):
return obj['methods'][name]
def Class_get(obj, attr):
return obj[attr]
def Class_set(obj, attr, value):
obj[attr] = value
def new(class_name, *args, **kw):
cls = load_class(class_name)
data = cls.new(*args, **kw)
methods = {'get': cls.__dict__.get('get', Class_get),
'set': cls.__dict__.get('set', Class_set),}
obj = {'data': data,
'name': class_name,
'methods': methods}
return obj
def get(obj, attr):
get_m = method(obj, 'get')
return get_m(obj['data'], attr)
def set(obj, attr, value):
set_m = method(obj, 'set')
return set_m(obj['data'], attr, value)
def is_a(obj, class_name):
return obj['name'] == class_name
from oop import *
def test_new():
a = new("Fruit", "apple", "red")
assert is_a(a, "Fruit")
assert get(a, 'color') == "red"
assert get(a, 'kind') == "apple"
set(a, 'color', 'orange')
set(a, 'kind', 'orange')
assert get(a, 'color') == "orange"
assert get(a, 'kind') == "orange"
def test_new_class_no_getset():
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')
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