54 lines
1.1 KiB
Plaintext
54 lines
1.1 KiB
Plaintext
|
Wrapping
|
||
|
=========
|
||
|
|
||
|
|
||
|
a wrapper can restrict, heritage cannot.
|
||
|
|
||
|
|
||
|
::
|
||
|
|
||
|
|
||
|
# ____________________________________________________________
|
||
|
# Base objects
|
||
|
|
||
|
class Element:
|
||
|
def __init__(self, name):
|
||
|
self.name = name
|
||
|
|
||
|
def __repr__(self):
|
||
|
return "<Element %s>"% self.name
|
||
|
|
||
|
# ____________________________________________________________
|
||
|
# Wrapped objects
|
||
|
|
||
|
class W_Element:
|
||
|
def __init__(self, name, wrap):
|
||
|
self._name = "wrapped_element" or name
|
||
|
self._w = wrap
|
||
|
|
||
|
def __getattr__(self, name):
|
||
|
return getattr(self._w, name)
|
||
|
|
||
|
def get_w(self, name):
|
||
|
return getattr(self._w, name)
|
||
|
|
||
|
def set_w(self, name, value):
|
||
|
return setattr(self._w, name, value)
|
||
|
|
||
|
_w = property (get_w, set_w)
|
||
|
|
||
|
def __repr__(self):
|
||
|
return "[W_Element %s]"% repr(self._name)
|
||
|
|
||
|
# ____________________________________________________________
|
||
|
# tests
|
||
|
|
||
|
e = Element('element 1')
|
||
|
w_e = W_Element('w_element 1', e)
|
||
|
e.a = 2
|
||
|
print w_e.a
|
||
|
print w_e
|
||
|
w_e.b = 5
|
||
|
print w_e.b
|
||
|
|