2.2 features

Jay O'Connor oconnor at bioreason.com
Tue Jul 31 17:23:33 CEST 2001

Quinn Dunkan wrote:
> On Sat, 28 Jul 2001 02:02:40 GMT, Guido van Rossum <guido at python.org> wrote:
> >Kirill Simonov <kirill at xyz.donetsk.ua> writes:
> >
> >> It would be nice to have 'in' operator for types:
> >>
> >> assert (num in int)
> >> assert (msg in str)
> This implies (at least to me) that 'in' would be a general shorthand for
> 'isinstance'.  But then I would sort of expect '3 in [1,2,3,4]' to behave like
> 'isinstance(3, [1,2,3,4])'
> Or you could just say that type objects implement a __contains__ method that
> does an isinstance.  But if classes and types are unified, that would get in
> the way of classes that define __contains__.  You'd have to think of it as a
> class method (or a metaclass method), which is in a different namespace than
> instance methods.  Or something.
> It's a nice syntax, but it seems a bit confusing to me.

One thing to think about is the Smalltalk distinction of #isMemberOf:
versus #isKindof::  One indicates that x is indeed an instance of the
class Y; the other indicates that x is an instance of y or any subclass
of y

| c | 
c := OrderedCollection new.
c isMemberOf: OrderedCollection. => returns true
c isMemeberOf: Collection. ==> returns false
c isKindOf: Collection.  => returns true because OrderedCollection is a
subclass of Collection

In Python, I could see use for the same thing....

c = Car()
c.isMemberOf (Car)  => returns 1
c.isMemberOf (Vehicle) => returns 0
c.isKindOf (Car) => returns 1

This may not work as well for primitives as there is no real
inheritance.  IntType and FloatType are not subclasses of a generic
NumberType so the following wouldn't work

def doSomething (aValue):
	assert (aValue.isKindOf(types.NumberType))

which would be very useful for asserting that aValue is any numberic
type ( I can think of the use of ListType, TupleTyupe and DictType all
being a SequenceType as well)

If you like the x in y syntax, than maybe that could be expanded to
include "of" as well

c = Car()
c in Car  => returns 1
c in Vehicle => returns 0
c of Vehicle => returns 1

Take care,
Jay O'Connor
oconnor at bioreason.com

More information about the Python-list mailing list