[Python-Dev] Sets are mappings?
Nick Coghlan
ncoghlan at gmail.com
Wed Dec 21 15:32:26 CET 2005
Michael Chermside wrote:
> Nick Coghlan writes:
>> Close enough to on-topic to stay here, I think. However, I tend to think of
>> the taxonomy as a little less flat:
>>
>> basecontainer (anything with __len__)
>> - set
>> - basemapping (anything with __getitem__)
>> - dict
>> - basesequence (anything which understands x[0:0])
>> - list
>> - tuple
>> - string
>> - unicode
>> - basearray (anything which understands x[0:0,])
>> - Numeric.array/scipy.array
<snip>
> So I have a counter-proposal. Let's NOT create a hierarchy of abstract
> base types for the elementary types of Python. (Even basestring feels
> like a minor wart to me, although for now it seems like we need it.)
Sorry - I meant to indicate that I didn't think the base classes were
necessary because the relevant checks already existed in a "does it behave
like one" sense:
def is_container(x):
try:
len(x)
return True
except (TypeError, AttributeError):
return False
def is_mapping(x):
return hasattr(x, "__getitem__")
def is_sequence(x):
try:
x[0:0]
return True
except LookupError:
return False
def is_multiarray(x):
try:
x[0:0,]
return True
except LookupError:
return False
I agree it's a definite tangent to the original topic :)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list