Python _ builder mode, python build Mode

Source: Internet
Author: User

Python _ builder mode, python build Mode

#! /Usr/bin/python #-*-coding: UTF-8-*-# builder base class PersonBuilder (): def BuildHead (self): pass def BuildBody (self ): pass def BuildArm (self): pass def BuildLeg (self): pass # fat class PersonFatBuilder (PersonBuilder): type = u 'fat' def BuildHead (self ): print U' build % s header '% self. type def BuildBody (self): print U' build % s body '% self. type def BuildArm (self): print U' build % s hand '% self. type def BuildLeg (self): print U' build % s '% self. type # slim class PersonThinBuilder (PersonBuilder): type = u 'skinny 'def BuildHead (self): print U' build % s header' % self. type def BuildBody (self): print U' build % s body '% self. type def BuildArm (self): print U' build % s hand '% self. type def BuildLeg (self): print U' build % s '% self. type # conductor class PersonDirector (): pb = None; def _ init _ (self, pb): self. pb = pb def CreatePereson (self): self. pb. buildHead () self. pb. buildBody () self. pb. buildArm () self. pb. buildLeg () def clientUI (): pb = PersonThinBuilder () pd = PersonDirector (pb) pd. createPereson () pb = PersonFatBuilder () pd = PersonDirector (pb) pd. createPereson () returnif _ name _ = '_ main _': clientUI ();

 


A problem with the generator in python

This function uses recursion. Since it is recursion, there must be conditions for ending recursion.

For example

The first time is:
X = [[1, 2], [3, 4], [5, 6]

The second dd (I:
X = [1, 2]

Third time:
X = 1

At this time, for I in x will obviously report an exception

Python generator problems

The generator is used to generate regular values.
The function that contains the yield statement is the generator. counter (5) indicates that the generator is called with the parameter start_at = 5 as the initial value, and the function object is assigned to count. count points to the generator object.
Each time the next method of the generator is called, the code is executed to the yield statement to return the value after yield. Therefore, the first call is executed to yield to return the value, the next time you call next, the yield statement is executed from the last execution. Therefore, you can use the while True statement in the generator to avoid endless loops.

The value of yield is None, so val is always None.

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.