-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python reference impl of MethodObject and ComputedAttribute
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |