Python aka. Smalltalk Lite?
Just van Rossum
just at letterror.com
Sun Feb 13 13:47:11 EST 2000
At 6:26 PM +0000 13-02-2000, Aahz Maruch wrote:
>In article <1e5wxbt.bq9v2nqzwbxuN%bparsia at email.unc.edu>,
>Bijan Parsia <bparsia at email.unc.edu> wrote:
>>
>>Here's are some whimper inducing things for me:
>> No class methods
>> No super
>
>Yeah, I've been able to work around these, but they're somewhat
>annoying. Note that I believe that Smalltalk has only single
>inheritance, so adopting super may be a bit difficult in Python.
Last time I tried to (not) get my head exploded about 'super' I realized
it's more subtle than that. I managed to implement it in Python, but it's
*majorly* ugly and inneficient. For entertainment, I've appended the code.
I'm not sure anymore if it's even correct...
Just
import sys
from types import FunctionType, MethodType
def getbasemethod(method):
assert type(method) == MethodType
for base in method.im_class.__bases__:
try:
basemethod = getattr(base, method.__name__)
except AttributeError:
pass
else:
assert type(basemethod) == MethodType
# XXX no can't do, see below:
#basemethod.im_self = method.im_self
return basemethod
else:
raise AttributeError, method.__name__
def findclass(klass, code):
for value in klass.__dict__.values():
if type(value) == FunctionType:
if value.func_code is code:
return klass
for base in klass.__bases__:
k = findclass(base, code)
if k:
return k
def super():
try:
1/0
except:
frame = sys.exc_info()[2].tb_frame.f_back
selfname = frame.f_code.co_varnames[0]
self = frame.f_locals[selfname]
klass = self.__class__
code = frame.f_code
methodname = frame.f_code.co_name
klass = findclass(klass, code)
if not klass:
raise AttributeError, methodname
# would be even better if we could bind it to "self"
return getbasemethod(getattr(klass, methodname))
class A:
def foo(self):
try:
print super()
except AttributeError:
print "no super method foo"
class B(A):
def foo(self):
s = super()
print s
s(self)
class C(B):
def foo(self):
s = super()
print s
s(self)
i = C()
i.foo()
More information about the Python-list
mailing list