<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Unfortunately that still requires two separate decorators, when I was
hoping there was a way to determine if I was handed a function or
method from within the same decorator.<br>
<br>
Seems like there really isn't, so two decorators is the way to go.<br>
Thanks,<br>
-David<br>
<br>
Carl Banks wrote:
<blockquote
cite="mid:ac046931-02d7-4067-a9a3-2f2cd9bc3bca@c9g2000yqm.googlegroups.com"
type="cite">
<pre wrap="">On Jun 29, 6:01 pm, David Hirschfield <a class="moz-txt-link-rfc2396E" href="mailto:dav...@ilm.com"><dav...@ilm.com></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">So is there
a pattern I can follow that will allow me to determine whether the
objects I'm given are plain functions or belong to a class?
Thanks in advance,
</pre>
</blockquote>
<pre wrap=""><!---->
class HomemadeUnboundMethod(object):
def __init__(self,func):
self.func = func
def __call__(self,*args,**kwargs):
print "is a function: %s" % self.func.func_name
return self.func(*args,**kwargs)
def __get__(self,obj,owner):
return HomemadeBoundMethod(obj,self.func)
class HomemadeBoundMethod(object):
def __init__(self,obj,func):
self.obj = obj
self.func = func
def __call__(self,*args,**kwargs):
print "is a method: %s" % self.func.func_name
return self.func(self.obj,*args,**kwargs)
class A(object):
@HomemadeUnboundMethod
def method(self): pass
@HomemadeUnboundMethod
def function(): pass
A().method()
function()
Just override the __call__ functions to do what you want the decorated
function to do. There are other little improvements you might make
(account for the owner parameter of __get__ for instance) but you get
the idea.
Carl Banks
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="80">--
Presenting:
mediocre nebula.
</pre>
</body>
</html>