Python-specific method and iteration mechanism instance analysis, python instance analysis

Source: Internet
Author: User

Python-specific method and iteration mechanism instance analysis, python instance analysis

The examples in this article describe the Python-specific methods and iteration mechanisms and share them with you for your reference. The specific analysis is as follows:

As we all know, the design philosophy of Python is "elegant", "clear", and "simple". It uses only one of the best methods to do one thing, this elegance naturally hides many details behind the scenes. For example, some objects are directly iterated using the for statement. Some global functions can act on many objects with common features, as well as generator decorators introspection and other features. Many of these implementations are implemented by using the internal special method of Python, while the external uses a unified global function for operations. In combination with some syntactic sugar, it makes Python easier to write, human intuition.

Python special method

Private method of the class: a method that starts with a double line, but does not end with a double underline;
Class exclusive method: starts and ends with a double underline and is often called by built-in functions;
Module private object: it must start with a single underline and cannot be imported to other modules;

#! /Usr/bin/env python # Python3 implement _ modeluprivate = 'module private' # The from module import * cannot be used to 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 (). _ test _ () # proprietary method, which is generally dedicated to the system. Do not use this new class method to name. _ People _ myprivate () # The private method is translated into this name to achieve private effect print (_ modeluprivate) ''' output call _ private: this is a private fun This module is private '''

Python iteration Mechanism

The iteratable object in Python is the object that implements the _ iter _ () method, while the _ iter _ () method returns an iterator object, the _ next _ () method must be implemented inside the iterator object. The iterator provides a unified interface for traversing the set, and can be operated directly using the for statement, which is very convenient. For some extremely large or infinite sets, the iterator avoids loading datasets at one time and is almost the only access method.

#! /Usr/bin/env python # Python3 implement 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 () constructor, which can accept the iteratable Object c = IterTest () print (next (c ), next (c), next (c) ''' output 1 2 3 [1, 2, 3] 1 2 3 '''

The Python generator actually returns an iterator. You can also use the next () function for it and use the for operation. The yield keyword makes it easier to create a generator.

#! /Usr/bin/env python # Python3 implement 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 '''

I hope this article will help you learn Python programming.


How to Use the iterator in python?

List = [1, 2, 3, 4, 5, 6]
For item in list:
Print item

What does iteration in python mean?

The above definition of mathematics: iteration formula refers to the use of the current value, instead of a formula, to calculate the next value, and then the next value into the formula, so reciprocating generation. For example, if x = (x + 2/x)/2, you can substitute x = 10 for x = (10 + 2/10)/2 = 5.1, x = (5.1 + 2/5.1)/2 = 2.746, and then 1.737, and so on.
In python, iterations can also be recursive calls. The following is an example:
Def f (n ):
If n = 0 or n = 1 or n = 2: return 1
Else: return f (n-1) + f (n-2)
This is a simple method for finding the nth Fibonacci number. Here we use an iteration. In addition, the Newton iteration method is used to obtain the number of operators of n based on the progressive effect. The following is an example:
Def f (guess ):
Return guess ** 2
Def fd (guess ):
Return 2 * guess
Def SquareRootNR (x, epsilon ):
Guess = x/2.0
Diff = f (guess)-x
Ctr = 1
While abs (diff)> epsilon and ctr <= 100:
Guess = guess-diff/fd (guess)
Diff = f (guess)-x
Ctr + = 1
Return guess

If you have any questions, please ask. If you are satisfied, please accept them!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.