search
HomeBackend DevelopmentPython TutorialPython专用方法与迭代机制实例分析

本文实例讲述了Python专用方法与迭代机制,分享给大家供大家参考之用。具体分析如下:

众所周知,Python 设计哲学是“优雅”、“明确”、“简单”,对于一件事只用一种最好的方法来做,而这种优雅在于背后很自然的隐藏了很多细节。比如对一些对象直接用for 语句来迭代,一些全局函数可以作用于很多具有共同特征的对象,还有生成器装饰器自省等特性。其中很多实现都是借助 Python  内部专用方法,而对外则使用统一的全局函数来进行操作,在配合一些语法糖,使得 Python 写起来愈发的方便,符合人的直觉。

Python 专用方法

类的私有方法:以双线划线开头,但是不以双下划线结尾的方法;
类的专有方法:以双下划线开头和结尾,常用来被内建函数调用;
模块私有对象:以单下划线开头,不能被导入到其他的模块中去;

#!/usr/bin/env python 
# Python3 实现 
_modeluprivate = '本模块私有'  #不能用 from module 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__()      # 专有方法,一般系统专用,自己的类方法不要用这种新式命名 
  a._People__myprivate() # 私有方法被对外被翻译成了这种名字,从而达到私有的效果 
  print(_modeluprivate) 
   
''''' 
输出 
call __private: This is a private fun 
This is a private fun 
本模块私有 
''' 

Python 迭代机制

Python 中的可迭代对象是实现了 __iter__() 方法的对象,而 __iter__() 方法返回一个迭代器对象,迭代器对象内部要实现 __next__() 方法。迭代器对外提供了一个统一的遍历集合的接口,并且可以直接用 for 语句来进行操作,非常的方便。对于一些特别大甚至无限的集合,迭代器避免了一次性将数据集载入,几乎是唯一的访问方法。

#!/usr/bin/env python 
# Python3 实现 
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()构造器,可以接受可迭代对象 
  c = IterTest() 
  print(next(c), next(c), next(c)) 
 
''''' 
输出 
1 2 3 [1, 2, 3] 
1 2 3 
''' 

Python 的生成器其实返回的也是一个迭代器,同样可以对其使用 next() 函数,对其使用 for操作,有了 yield 关键字使得创建生成器更加的方便。

#!/usr/bin/env python 
# Python3 实现 
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)) 
 
''''' 
输出 
1 2 3 1 2 3 
'''

希望本文所述对大家Python程序设计的学习有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft