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