Python Snippets(3)

1.
*attr()

  1. class myClass(object):
  2.     def __init__(self):
  3.         self.foo = 100
  4. myInst = myClass()
  5. print hasattr(myInst, 'foo')
  6. print getattr(myInst, 'foo')
  7. print hasattr(myInst, 'bar')
  8. #print getattr(myInst, 'bar')
  9. print getattr(myInst, 'bar', 'oooops')
  10. setattr(myInst, 'bar', 'my attr')
  11. print getattr(myInst,'bar')
  12. print dir(myInst)
  13. print getattr(myInst,'bar')
  14. delattr(myInst, 'foo')
  15. print dir(myInst)

2.
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode[……]

Read more

Python snippets (2)

1.
Generators

  1. def simpleGen():
  2.     yield 1
  3.     yield '2--->punch!'
  4.  
  5. myG = simpleGen()
  6. '''
  7. myG.next()
  8. myG.next()
  9. myG.next() #Exception
  10. '''
  11.  
  12. for eachItem in simpleGen():
  13.     print eachItem,
  14.  
  15. print '-----------------------------------------'
  16. from random import randrange
  17. def randGen(aList):
  18.     while len(aList) > 0:
  19.         yield aList.pop(randrange(len(aList)))# Just like list.pop() and random.choice()
  20.  
  21. for item in randGen(['rock', 'paper', 'scissors']):
  22.     print item

2.

  1. import longmodulename
  2. short = longmodulename
  3. del longmodulename
  4.  
  5. from cgi import FieldStorage as form

3.

1677241673[……]

Read more

宋之韵

《宋之韵》文字解说词

第一集 宋词概览
  一说宋词,我们立刻就会想到晏殊的,无可奈何花落去,似曾相识燕归来。想到欧阳修的,人生自是有情痴,此事不关风与月。想到晏几道的,今宵剩把银釭照,犹[……]

Read more