This article describes the Python-specific method and iterative mechanism, share for everyone to use for reference. The specific analysis is as follows:
As we all know, Python design philosophy is "elegant", "clear", "simple", for one thing only one of the best way to do, and this elegance lies behind the natural hidden a lot of details. For example, some objects are iterated directly with a for statement, and some global functions can be used for many objects with common characteristics, as well as the characteristics of the builder adorner introspection. Many of these implementations are based on Python's internal proprietary methods, while the external use of a unified global function to operate, in conjunction with some syntactic sugar, making Python more convenient to write, consistent with human intuition.
Python-specific methods
The private method of a class: A method that starts with a double line, but does not end with a double underline;
The proprietary method of a class: The beginning and end of a double underline, commonly used to be called by the built-in function;
Module Private object: Begins with a single underline and cannot be imported into other modules;
#!/usr/bin/env Python
# Python3 implementation
_modeluprivate = ' This module private ' #不能用 from module import * Importing
class people (): C5/>def __myprivate (self):
print ("This be a private fun")
def __test__ (self):
print (' Call __private: ', end = ')
self.__myprivate ()
if __name__ = = ' __main__ ':
a = people ()
a.__test__ () # Proprietary method, general system-specific, Their own class methods do not use this new name
a._people__myprivate () # Private method has been translated into such names, thereby achieving the proprietary effect of
print (_modeluprivate)
' "
output Call
__private:this be a private fun
This is a private fun
""
Python iterative mechanism
An iterative object in Python is an object that implements the __iter__ () method, while the __iter__ () method returns an iterator object that implements the __next__ () method internally. Iterators provide a unified interface to traverse the collection, and can be used directly for the statement to operate, very convenient. For some particularly large or even infinite sets, iterators avoid having to load a dataset at once, which is almost the only way to access it.
#!/usr/bin/env Python
# Python3 Implementation
Class Itertest ():
def __init__ (self):
self.a = 0
def __iter__ ( Self): return
self
def __next__ (self):
SELF.A + 1
if SELF.A > 3:
raise Stopiteration
Return SELF.A
If __name__ = = ' __main__ ':
a = Itertest () to
I in a:
print (i,end= ')
B = itertest ()
print (list (b)) # list () constructor, can accept the iterator object
c = itertest ()
print (Next (c), Next (c), Next (c))
'
outputs
1 2 3 [1, 2, 3]
1 2 3
Python's generator actually returns an iterator, as well as using the next () function, using a for operation for it, and having the yield keyword makes it easier to create generators.
#!/usr/bin/env Python
# Python3 implementation
def fungenerate ():
yield 1
yield 2
yield 3
if __name_ _ = = ' __main__ ':
a = Fungenerate ()
for I in A:
print (i, end= ')
B = fungenerate ()
print (Next (b) , Next (b), next (b))
'
output
1 2 3 1 2 3
'
I hope this article will help you with your study of Python programming.