[Python-3000] PEP 3133: Introducing Roles (original) (raw)

Phillip J. Eby pje at telecommunity.com
Mon May 14 18:42:27 CEST 2007


At 10:36 PM 5/13/2007 -0700, Collin Winter wrote:

2. Querying instances ::

if performs(myelf, Thieving): ...

-1 on using any function other than isinstance() for this.

Rationale: isinstance() makes the code smell of inspection more obvious, where another function name makes it seem like you are doing something harmless. In reality, performs() testing (or any other kind of interface testing) using if-then is always harmful in library code.

The second argument to performs() may also be anything with a _contains_() method, meaning the following is legal: ::

if performs(myelf, set([Thieving, Spying, BoyScout])): ... Like isinstance(), the object needs only to perform a single role out of the set in order for the expression to be true.

Right, so let's just use isinstance(). Likewise, issubclass() for checking whether instances of a class perform a role. (And if issubclass() works, then roles will also be usable by PEP 3124 generic functions without any additional effort.)



More information about the Python-3000 mailing list