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.