On 6/27/10 8:04 PM, Steven W. Orr wrote:
On 6/27/2010 10:25 PM, Stephen Hansen wrote:
On 6/27/10 7:09 PM, Steven W. Orr wrote:
So, my question is, what value can I use as the 2nd arg to isinstance
to see if
foo is a function? And while I'm on the subject, what types does
isinstance not
support?

Does it have to be a function? -- There's quite a few things which are
function-y enough that you really should accept them even if they may
not be technically a /function/. Like a class instance with the __call__
method defined wrapping a function as a decorator. Or a bound method,
really.

In Python 2.x, you can use callable(fun) -- that goes away in Py3
though. But personally, I think this is the wrong approach.

Ok, you just asked great questions, and your questions were based on what I
didn't tell you.

I'm trying to teach myself about how __metaclass__ can be used as a substitute
for LSD. ;-)

[snip]

Ah. In that case, bearing in mind this won't work for Python 3, I would use callable().

So, isinstance(x, (int, str, tuple)); but don't rely on isinstance for function. Its just not appropriate, almost universally, to really be that specific.

When someone esays they want to accept a function, what they *really* mean in my experience is they want some sort of object that you can put ()'s on after, and usually with some sort of agreement: it'll accept X args, and return Y things.

You can't readily test for those agreements, but you can test to see if it'll accept ()'s after and be all function-y. Doing callable(ff) will do that for you.

--

   ... Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

P.S. The removal of callable is something I don't understand in Python 3: while generally speaking I do really believe and use duck typing, I too have on occassion wanted to dispatch based on 'is callable? do x'. Sometimes its not convenient to do so via duck typing. Its rare. But it is there. That isinstance()/issubclass got a boost in power with the ABC's and registering, while at the same time the ability to introspect about the function-y callable-y ness of a function was removed? Makes no sense to me. But alas!
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to