Python专用方法与迭代机制实例分析
本文实例讲述了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程序设计的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
