Python specific methods and iteration mechanism case analysis

  • 2020-04-02 14:04:05
  • OfStack

This article is an example of a Python specific method and iteration mechanism. Specific analysis is as follows:

As we all know, the Python design philosophy is "elegant", "clear", "simple", and only the best way to do one thing, and this elegance lies in the fact that many details are naturally hidden behind it. For example, some objects can be iterated directly with a for statement, some global functions can be applied to many objects with common characteristics, and some features such as generator decorator introspection. Many of these implementations rely on Python   Internally specialized methods are used, while externally unified global functions are used for operation. In combination with some syntactic sugar, Python is more convenient to write, which conforms to human intuition.

Python-specific methods

Class: a method that begins with a double underscore but does not end with a double underscore.
Class methods: start and end with a double underscore, often used to be called by built-in functions;
Module private object: starts with a single underscore and cannot be imported into other modules.


#!/usr/bin/env python 
# Python3  implementation  
_modeluprivate = ' This module is private '  # Can't use  from module import *  The import  
 
class People(): 
  def __myprivate(self): 
    print("This is a private fun") 
  def __test__(self): 
    print('call __private: ',end='') 
    self.__myprivate() 
 
if __name__ == '__main__': 
  a = People() 
  a.__test__()      #  Proprietary methods, general system - specific, your own class methods do not use this new name  
  a._People__myprivate() #  Private methods are translated externally into such names to achieve the effect of being private  
  print(_modeluprivate) 
   
''''' 
 The output  
call __private: This is a private fun 
This is a private fun 
 This module is private  
''' 

Python iteration mechanism

The iterable object in Python is the object that implements the iteriter__ () method, and the iteriter__ () method returns an iterator object, with the iterator object implementing the iterator () method internally. Iterators provide a unified interface for traversing the set, and can be operated directly with the for statement, which is very convenient. For some extremely large or even infinite collections, iterators are almost the only access method that avoids loading the dataset all at once.


#!/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() 
  for i in a: 
    print(i,end=' ') 
  b = IterTest() 
  print(list(b)) # list() The constructor , Iterable objects are acceptable  
  c = IterTest() 
  print(next(c), next(c), next(c)) 
 
''''' 
 The output  
1 2 3 [1, 2, 3] 
1 2 3 
''' 

Python's generator actually returns an iterator, which you can also use the next() function on, and the for operation on, and the yield keyword makes it easier to create a generator.


#!/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)) 
 
''''' 
 The output  
1 2 3 1 2 3 
'''

I hope that this article has helped you to learn Python programming.


Related articles: