Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The big advantage is that built-in functions (and operators) can apply extra logic when appropriate, beyond simply calling the special methods. For example, <code>min</code> can look at several arguments and apply the appropriate inequality checks, or it can accept a single iterable argument and proceed similarly; <code>abs</code> when called on an object without a special method <code>__abs__</code> could try comparing said object with 0 and using the object change sign method if needed (though it currently doesn't); and so forth.</p> <p>So, for consistency, all operations with wide applicability must always go through built-ins and/or operators, and it's those built-ins responsibility to look up and apply the appropriate special methods (on one or more of the arguments), use alternate logic where applicable, and so forth.</p> <p>An example where this principle wasn't correctly applied (but the inconsistency was fixed in Python 3) is "step an iterator forward": in 2.5 and earlier, you needed to define and call the non-specially-named <code>next</code> method on the iterator. In 2.6 and later you can do it the right way: the iterator object defines <code>__next__</code>, the new <code>next</code> built-in can call it <em>and</em> apply extra logic, for example to supply a default value (in 2.6 you can still do it the bad old way, for backwards compatibility, though in <code>3.*</code> you can't any more).</p> <p>Another example: consider the expression <code>x + y</code>. In a traditional object-oriented language (able to dispatch only on the type of the leftmost argument -- like Python, Ruby, Java, C++, C#, &amp;c) if <code>x</code> is of some built-in type and <code>y</code> is of your own fancy new type, you're sadly out of luck if the language insists on delegating all the logic to the method of <code>type(x)</code> that implements addition (assuming the language allows operator overloading;-).</p> <p>In Python, the <code>+</code> operator (and similarly of course the builtin <code>operator.add</code>, if that's what you prefer) tries x's type's <code>__add__</code>, and if that one doesn't know what to do with <code>y</code>, then tries y's type's <code>__radd__</code>. So you can define your types that know how to add themselves to integers, floats, complex, etc etc, as well as ones that know how to add such built-in numeric types to themselves (i.e., you can code it so that <code>x + y</code> and <code>y + x</code> both work fine, when <code>y</code> is an instance of your fancy new type and <code>x</code> is an instance of some builtin numeric type).</p> <p>"Generic functions" (as in PEAK) are a more elegant approach (allowing any overriding based on a combination of types, <em>never</em> with the crazy monomaniac focus on the leftmost arguments that OOP encourages!-), but (a) they were unfortunately not accepted for Python 3, and (b) they do of course require the generic function to be expressed as free-standing (it would be absolutely crazy to have to consider the function as "belonging" to any single type, where the whole POINT is that can be differently overridden/overloaded based on arbitrary combination of its several arguments' types!-). Anybody who's ever programmed in Common Lisp, Dylan, or PEAK, knows what I'm talking about;-).</p> <p>So, free-standing functions and operators are just THE right, consistent way to go (even though the lack of generic functions, in bare-bones Python, does remove <em>some</em> fraction of the inherent elegance, it's still a reasonable mix of elegance and practicality!-).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload