2.2 features
Marcin 'Qrczak' Kowalczyk
qrczak at knm.org.pl
Sat Jul 28 17:03:50 EDT 2001
28 Jul 2001 18:33:33 GMT, Quinn Dunkan <quinn at retch.ugcs.caltech.edu> pisze:
> 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])'
If you forget about 'isinstance' then everything is ok, right?
'in' performs some membership test whose details depend on the second
argument. It should not be more confusing that '+' being either addition
of numbers and concatenation of sequences. We don't expect "a"-(-"b")
to be equivalent to "a"+"b" just because 3-(-4) is equivalent to 3+4.
Now thow in 'isinstance' as an old way to spell a specific membership
test (for types and classes) which doesn't have to be learned about
at all.
> 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__.
It indeed shows a flaw in unifying "attributes of instances which
are defined by their classes" with "attributes of class objects
themselves".
For example many objects are hashed by what o.__hash__() returns,
but not type objects, because their __hash__ is something completely
different (it expects an argument and hashes it instead of the
receiver). Similarly for most magic methods.
It more or less works because intersection of proper attributes of
type objects and attributes which are always polymorphically extracted
using names (and not slots in the representation of type objects)
happens to be empty.
This is so hardwired into Python and pushed by type/class unification
that I don't believe it can be avoided without redesigning the language
from scratch.
This particular case of __contains__ is not different than e.g.
__hash__ or __str__. You can't redefine in Python how your class object
(or type object in Python-2.2) will be hashed or printed, you won't
be able to override the membership test either (and you can't now
AFAIK) - unless something solves the generic problem of putting class
attributes and static instance attributes in one namespace.
--
__("< Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZASTĘPCZA
QRCZAK
More information about the Python-list
mailing list