Thomas Wouters <thomas <at> python.org> writes: > On 5/7/06, Talin <talin <at> acm.org> wrote: > > > How about instead of 'callable', an "isFunction' test to go alongwith > > 'isSequence' and 'isMapping'. (You aren't getting rid of those > > - I hope...?) > > He is, and for the same reasons as callable(): there is no way to > tell whether an object is a mapping or a sequence (considering > Python object of both 'types' implement __getitem__, and that's > it. If you don't believe me, ask isSequence about UserDict.). > You can guess, but guessing is not what Python should > do. If you want to make your library guess, feel free (and > document it), but it has no place in language specifics.
Some of the best Python libraries out there (IMHO) have functions that test their arguments to determine if they are a sequence or not. As an example, check out section 4: "Content Producing Constructs" in the Kid docs: http://kid.lesscode.org/language.html I'll summarize it here: In Kid, all content producing constructs, such as ${name} and py:attr, use the same set of rules for what types of objects may result from the Python expression they contain. str, unicode - The string is inserted as XML CDATA. ElementTree.Element - When an ElementTree.Element is referenced from a content producing construct, the item is inserted into the document literally, i.e. it is not encoded as text, but becomes part of the output structure. sequence - If a sequence type (list, tuple, or other iterable) is referenced, the rules are applied to each of the items in the sequence. For example, you could reference a list containing an Element and a string. Other - If the result of evaluating the expression is any other type, an attempt is made to coerce the value to unicode as if by calling unicode(expr) and processing continues as if the object were a string or unicode object initially. My reaction on reading this the first time was "Wow, what a sensible API!" Lets say we outlaw the use of isSequence - how do you propose to implement this type of pattern? Or are you saying that this pattern is bad style? If you want another example, look at Scons. And I agree that isSequence is theoretically ambiguous - but I've never run across a case where it didn't do the right thing. Yes, there are edge-cases that can return the wrong answer, but in practice these edge cases don't tend to happen, at least not in my experience, because most of the time when someone passes in a sequence as an argument, it really is a sequence, not some pseudo-combination of sequence and something else. I would hate to be told that I'm not allowed to test whether something is a sequence or not. -- Talin _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com