7.9. Set Type Built-in Methods7.9.1. Methods (All Set Types)We have seen the operator equivalents to most of the built-in methods, summarized in Table 7.4.
The one method without an operator equivalent is copy(). Like the dictionary method of the same name, it is faster to create a copy of the object using copy() than it is using a factory function like set(), frozenset(), or dict(). 7.9.2. Methods (Mutable Sets Only)Table 7.5 summarizes all of the built-in methods that only apply to mutable sets, and similar to the methods above, we have already seen most of their operator equivalents.
The new methods here are add(), remove(), discard(), pop(), and clear(). For the methods that take an object, the argument must be hashable. 7.9.3. Using Operators versus Built-in MethodsAs you can see, there are many built-in methods that have near-equivalents when using operators. By "near-equivalent," we mean that there is one major difference: when using the operators, both operands must be sets while for the methods, objects can be iterables too. Why was it implemented this way? The official Python documentation states that "[this] precludes error-prone constructions like set('abc') [and] 'cbs' in favor of the more readable set('abc').intersection('cbs')." |
Sunday, October 25, 2009
Section 7.9. Set Type Built-in Methods
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment