From: Chris Rebert on
On Sun, Jun 27, 2010 at 9:54 PM, Stephen Hansen
<me+list/python(a)ixokai.io> wrote:
> On 6/27/10 9:30 PM, alex23 wrote:
>> Stephen Hansen<me+list/pyt...(a)ixokai.io>  wrote:
>>> 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!
>>
>> There's always: isinstance(<object>, collections.Callable)
>
> What the hell? When did that show up? o.O (Did I not pay attention enough
> during the ABC conversations? It seemed so boring).
>
> A) how is Callable a collection, in any way shape or form?

Completely agree, see my prior reply.

> And B) does that
> really return True for everything callable-esque? (I don't have a 3.x to
> play with on this temporary computer)

You don't even need 3.x; it was added in 2.6. The new magic of
__instancecheck__ makes it possible.
See http://docs.python.org/reference/datamodel.html#customizing-instance-and-subclass-checks

Cheers,
Chris
--
Very cool new magic, eh?
http://blog.rebertia.com
From: Stephen Hansen on
On 6/27/10 10:09 PM, alex23 wrote:
> Stephen Hansen<me+list/pyt...(a)ixokai.io> wrote:
>> What the hell? When did that show up? o.O (Did I not pay attention
>> enough during the ABC conversations? It seemed so boring).
>
> The PEPs& post-release docs detailing Py3 changes were worth reading,
> it's noted in the sections on changes to built-ins:
>
> http://www.python.org/dev/peps/pep-3100/
> http://docs.python.org/py3k/whatsnew/3.0.html

I've actually read nearly all of that multiple times-- I tend to idly
re-read such things. But not everything sticks (as its usually late like
now and I'm multitasking and just exploring curiously). Callable in
collections (and the ABC's in collections-- I swear I thought they were
in a module of their own) totally didn't sink in.

>> A) how is Callable a collection, in any way shape or form? And B) does
>> that really return True for everything callable-esque? (I don't have a
>> 3.x to play with on this temporary computer)
>
> A) I was tempted to say "it's a collection of code" :) But really, the
> role of the collections model has expanded in 3.x to also provide a
> repository for ABCs:
>
> "In addition to containers, the collections module provides some ABCs
> (abstract base classes) that can be used to test whether a class
> provides a particular interface, for example, whether it is hashable
> or a mapping."
>
> http://docs.python.org/py3k/library/collections.html

The documentation might say something crazy, but just because its
*officially* crazy doesn't make it any less actually crazy.

Just because most of the ABC's are vaguely collection-related, doesn't
mean we should put them all into the collections module if there's some
which aren't really collection-ish. It argues for either putting
different ABC's in different places (say, Callable in functools? Who
knows) or just putting them all in a single module. I dunno which: its
far too late to do anything about now, but yeah.

Re-defining a module to suddenly include various random tidbits which
aren't even really related to that module's purpose counts as crazy in
my book.

I don't often think, "That's crazy." about Python, too. And although I'm
not really in a place to use Python 3 yet, I do try to learn more and
more of it as time goes on and play with it (and experiment with it) so
that when I'm able, I'll be ready. And so far I've been largely very
pleased, although Python 2 was rarely crazy (print >>crazy, "not
withstanding") I've been pleased with the less odd edges as I explore Py3.

Until now.

Oh well, nothing can be perfect. :)

--

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

From: Hans Mulder on
alex23 wrote:
> Stephen Hansen <me+list/pyt...(a)ixokai.io> wrote:
>> 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!
>
> There's always: isinstance(<object>, collections.Callable)

There's also: hasattr(<object>, '__call__'). It works in both 2.x and 3.x.


-- HansM
From: alex23 on
Hans Mulder <han...(a)xs4all.nl> wrote:
> There's also: hasattr(<object>, '__call__').  It works in both 2.x and 3.x.

Good work, Hans. I do find that to be a more pythonic approach,
personally, being more concerned with an object's ability than its
abstract type.