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) '. '}
__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.
__setattr__() method is called whenever you assign a value to an attribute.
__delattr__() method is called whenever you delete an attribute.
-__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.
+__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'
__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.)
-__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.)
+__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.)
+__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.)
__getattr__() method is called to provide a computed value.
__getattr__() method will no longer be called to provide a value for dyn.color, because dyn.color is already defined on the instance.
-☞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 whetherx <= 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 whetherx <= y, Python will not call__lt__()and__eq__()in sequence. It will only call the__le__()method.