Skip to content

Commit

Permalink
add python reference impl of MethodObject and ComputedAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Mar 20, 2013
1 parent 2f8edb2 commit 7213312
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/ComputedAttribute/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
from _ComputedAttribute import *
from ExtensionClass import Base


class ComputedAttribute(Base):
"""ComputedAttribute(callable) -- Create a computed attribute"""

def __init__(self, func, level=0):
if level > 0:
func = ComputedAttribute(func, level - 1)
self.callable = func
self.level = level

def __of__(self, inst):
func = self.__dict__['callable']
if self.level:
return func
return func(inst)


try:
from _ComputedAttribute import *
except:
pass
25 changes: 24 additions & 1 deletion src/MethodObject/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
from _MethodObject import *
import new
from ExtensionClass import Base


class Method(Base):
"""Base class for objects that want to be treated as methods
The method class provides a method, __of__, that
binds an object to an instance. If a method is a subobject
of an extension-class instance, the the method will be bound
to the instance and when the resulting object is called, it
will call the method and pass the instance in addition to
other arguments. It is the responsibility of Method objects
to implement (or inherit) a __call__ method.
"""

def __of__(self, inst):
return new.instancemethod(self, inst)


try:
from _MethodObject import *
except:
pass

0 comments on commit 7213312

Please sign in to comment.