2.2 features

Charlie Clark charlie at begeistert.org
Fri Aug 3 10:05:23 EDT 2001


Olaf said,

Not bad! How about
  aClass.has_instance(anInstance)
and
  aBaseclass.has_subclass(aSubclass),
then?

This would resemble dictionary.has_key(...).

I've just read the whole thread to try and get to grips with it.
"isinstance" and "issubclass" are plain ugly and confusing to read and a
lot of the debate has centred around consistency with the rest of the
language. I suppose it's trying to square the circle which is so
difficult...
I understand the problem like this: Classes keep lists of their
sub-classes *and* of their instances and "in" is a keyword which checks
for membership in sequences.

class Programmer
	pass

class GoodProgrammer(Programmer)
	pass

class BadProgrammer(Programmer)

guido = GoodProgrammer()
charlie = BadProgrammer()

Is this the right way of thinking about it? If so we could then ask our
classes to list their instances and subclasses and apply membership
tests to them.

Programmer.instance()
>>> ['']
Programmer.subclasses()
>>> ['GoodProgrammer', 'BadProgrammer']
GoodProgrammer.instance()
>>>'Guido'
GoodProgrammer.subclass()
>>>['']
etc.

This would then let us test for membership in class instances or
subclasses.
Guido in Programmer.instance()
>>>0
Guido in GoodProgrammer.instance()
>>>1
Guido in Programmer.subclass()
>>>0
Charlie in BadProgrammer.instance()
>>>very much so

I think the syntax might not be correct but this would seem to me to be
a clean way of checking for membership without introduing new key words,
new attributes subclass and instance (or should that be __subclass__,
__instance___?) which are only handles for object counters and "in"
would be preserved as the shorthand for the contains method. Not so sure
how this would apply to the built-ins. Something like this?
>>>x = 4
>>>x in int.instance()
>>>1

What thinketh yee?

I still don't get generators btw. What does 'yield' do that 'return'
can't

Charlie



More information about the Python-list mailing list