From 84315da3573a0a502defc16d48d058026a2faea3 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 22 Oct 2009 15:49:44 -0400 Subject: [PATCH] typo --- special-method-names.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/special-method-names.html b/special-method-names.html index 86cc156..c9be80a 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -128,7 +128,7 @@ h3:before{counter-increment:h3;content:'B.' counter(h2) '.' counter(h3) '. '}
  • If your class defines a __getattr__() method, Python will call it only after looking for the attribute in all the normal places. If an instance x defines an attribute color, x.color will not call x.__getattr__('color'); it will simply return the already-defined value of x.color.
  • The __setattr__() method is called whenever you assign a value to an attribute.
  • The __delattr__() method is called whenever you delete an attribute. -
  • The __dir__() method is useful if you define a __getattr__() or __getattribute__() method. Normally, calling dir(x) would only list the regular attributes and methods. If your __getattr()__ method handles a color attribute dynamically, dir(x) would not list color as one of the available attributes. Overriding the __dir__() method allows you to list color as an available attribute, which is helpful for other people who wish to use your class without digging into the internals of it. +
  • The __dir__() method is useful if you define a __getattr__() or __getattribute__() method. Normally, calling dir(x) would only list the regular attributes and methods. If your __getattr__() method handles a color attribute dynamically, dir(x) would not list color as one of the available attributes. Overriding the __dir__() method allows you to list color as an available attribute, which is helpful for other people who wish to use your class without digging into the internals of it.

    The distinction between the __getattr__() and __getattribute__() methods is subtle but important. I can explain it with two examples: @@ -148,8 +148,8 @@ h3:before{counter-increment:h3;content:'B.' counter(h2) '.' counter(h3) '. '} >>> dyn.color 'LemonChiffon'

      -
    1. The attribute name is passed into the __getattr()__ method as a string. If the name is 'color', the method returns a value. (In this case, it’s just a hard-coded string, but you would normally do some sort of computation and return the result.) -
    2. If the attribute name is unknown, the __getattr()__ method needs to raise an AttributeError exception, otherwise your code will silently fail when accessing undefined attributes. (Technically, if the method doesn’t raise an exception or explicitly return a value, it returns None, the Python null value. This means that all attributes not explicitly defined will be None, which is almost certainly not what you want.) +
    3. The attribute name is passed into the __getattr__() method as a string. If the name is 'color', the method returns a value. (In this case, it’s just a hard-coded string, but you would normally do some sort of computation and return the result.) +
    4. If the attribute name is unknown, the __getattr__() method needs to raise an AttributeError exception, otherwise your code will silently fail when accessing undefined attributes. (Technically, if the method doesn’t raise an exception or explicitly return a value, it returns None, the Python null value. This means that all attributes not explicitly defined will be None, which is almost certainly not what you want.)
    5. The dyn instance does not have an attribute named color, so the __getattr__() method is called to provide a computed value.
    6. After explicitly setting dyn.color, the __getattr__() method will no longer be called to provide a value for dyn.color, because dyn.color is already defined on the instance.
    @@ -668,7 +668,7 @@ class FieldStorage:
    -

    If you define a __lt__() method but no __gt__() method, Python will use the __lt__() method with operands swapped. However, Python will not combine methods. For example, if you define a __lt__() method and a __eq()__ method and try to test whether x <= y, Python will not call __lt__() and __eq()__ in sequence. It will only call the __le__() method. +

    If you define a __lt__() method but no __gt__() method, Python will use the __lt__() method with operands swapped. However, Python will not combine methods. For example, if you define a __lt__() method and a __eq__() method and try to test whether x <= y, Python will not call __lt__() and __eq__() in sequence. It will only call the __le__() method.

    Classes That Can Be Serialized