Such __slots__ see similar shaped like __xxx__ variable or function name it should be noted that these are special purpose in Python
Python There are many special-purpose functions that can help us custom class
__str__
Student define a class, an instance of printing
>>> class Student(object): ... def __init__(self,name): ... self.name=name ... >>> print(Student('Zhangsan')) <__main__.Student object at 0x7f8a4830a748>
Print out <__ main __. Student object at 0x7f8a4830a748> does not look good, non-intuitive
How nice to print it? Only you need to define __str __ () method to return a nice string on it
>>> class Student(object): ... def __init__(self,name): ... self.name=name ... def __str__(self): ... return 'Student object(name:%s)' % self.name ... >>> >>> print(Student('Zhangsan')) Student object(name:Zhangsan)
Examples of such print out, not only good-looking, and is easy to see inside instance important data
But if the direct knock variables in the terminal instead of print, or print out the same does not look good
>>> Student('Zhangsan') <__main__.Student object at 0x7f8a4830a8d0>
This is because the call is not directly displayed variables __str__()
, but __repr__()
, the difference is __str__()
returned to the user to see the string and __repr__()
returns a string program developers to see, that is, __repr__()
for debugging services.
The solution is to define a second __repr__()
. But usually __str__()
, and __repr__()
the code is the same, so there is a lazy wording:
>>> class Student(object): ... def __init__(self,name): ... self.name=name ... def __str__(self): ... return 'Student object(name:%s)' % self.name ... __repr__=__str__ ... >>> >>> Student('Zhangsan') Student object(name:Zhangsan)
__iter__
If you want a class to be used in for..in loops, similar to that list or tuple, you must implement a __iter __ () method, which returns an iterator object, and then, Python for loop iteration will continue to call the object __next __ () method to get the next cycle of value, until it encounters an error StopIteration exit the loop
Fib class (Object): DEF __init __ (Self): # initialize two counters self.a, self.b = 0, 1 DEF the __iter __ (Self): # iteration object instance itself, to return to their return Self DEF the __next __ (Self) : self.a, self.b, self.b, =, + self.b, self.a # set loop exit condition IF self.a> 10000: The raise the StopIteration () return self.a for in n-Fib (): Print (n- )
Export
>>> for n in Fib(): ... print(n) ... 1 1 2 3 5 ... 46368 75025
__grtitem__
Examples Although Fib can function in a for loop, and looks somewhat like a list, however, to use it as a list or not, for example, take the first 5 elements
>>> Fib()[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Fib' object is not subscriptable
To remove the above list as performance index of the element in accordance with the need to achieve the __getitem __ () method
special_getitem.py
class Fib(object): def __getitem__(self,n): a,b=1,1 for x in range(n): a,b=b,a+b return a
Now, you can press any number to access the column marked one of the
>>> f[0] 1 >>> f[1] 1 >>> f[100] 573147844013817084101
However, there is a list of methods corresponding to the slice error determined Fib
>>> f[1:5] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in __getitem__ TypeError: 'slice' object cannot be interpreted as an integer