????????eval
????eval??????????????python??????(????????????????)?? ?????????????????????У? ???????н???????
?????????????????????
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????def test_first():
????return 3
????def test_second(num):
????return num
????action = {  # ????????????sandbox
????"para": 5??
????"test_first" : test_first??
????"test_second": test_second
????}
????def test_eavl(): 
????condition = "para == 5 and test_second(test_first) > 5"
????res = eval(condition?? action)  # ????condition??????action???????????
????print res
????if __name__ == '_
????exec
????exec??Python?л?????????? ???????None?? eval??????д????????????
????exec??eval????д?????? ????????????????????
????????????????? ?????compile(source?? ‘<string>’?? mode)????????? mode?????exec??eval
????#!/usr/bin/env python
????# -*- coding: utf-8 -*-
????def test_first():
????print "hello"
????def test_second():
????test_first()
????print "second"
????def test_third():
????print "third"
????action = {
????"test_second": test_second??
????"test_third": test_third
????}
????def test_exec():
????exec "test_second" in action
????if __name__ == '__main__':
????test_exec()  # ?????????н??
????getattr
????getattr(object?? name[?? default])?????????????????????????????????????????????????????????????????????????????磬 getattr(x?? ‘foobar’) ????? x.foobar?? ????????????????????????????????????????? AttributeError ??
????# ??÷???
????class TestGetAttr(object):
????test = "test attribute"
????def say(self):
????print "test method"
????def test_getattr():
????my_test = TestGetAttr()
????try:
????print getattr(my_test?? "test")
????except AttributeError:
????print "Attribute Error!"
????try:
????getattr(my_test?? "say")()
????except AttributeError: # ??и?????? ????????????????????
????print "Method Error!"
????if __name__ == '__main__':
????test_getattr()
????# ??????
????test attribute
????test method
?????????д???
????def process_command_line(argv):
????"""
????Return a 2-tuple: (settings object?? args list).
????`argv` is a list of arguments?? or `None` for ``sys.argv[1:]``.
????"""
????if argv is None:
????argv = sys.argv[1:]
????# initialize the parser object:
????parser = optparse.OptionParser(
????formatter=optparse.TitledHelpFormatter(width=78)??
????add_help_option=None)
????# define options here:
????parser.add_option(      # customized description; put --help last
????'-h'?? '--help'?? action='help'??
????help='Show this help message and exit.')
????settings?? args = parser.parse_args(argv)
????# check number of arguments?? verify values?? etc.:
????if args:
????parser.error('program takes no command-line arguments; '
????'"%s" ignored.' % (args??))
????# further process settings & args if necessary
????return settings?? args
????def main(argv=None):
????settings?? args = process_command_line(argv)
????# application code here?? like:
????# run(settings?? args)
????return 0        # success
????if __name__ == '__main__':
????status = main()
????sys.exit(status)
??????дcsv???
????# ??csv?ж??????? ??????????????????
????import csv
????with open('data.csv'?? 'rb') as f:
????reader = csv.reader(f)
????for row in reader:
????print row
????# ??csv???д??
????import csv
????with open( 'data.csv'?? 'wb') as f:
????writer = csv.writer(f)
????writer.writerow(['name'?? 'address'?? 'age'])  # ????д??
????data = [
????( 'xiaoming '??'china'??'10')??
????( 'Lily'?? 'USA'?? '12')]
????writer.writerows(data)  # ????д??
?????????????????
?????????????????? ???????????? ??????????


??????????????
??????????????? ?????????????????
????>>> name = "andrew"
????>>> "my name is {name}".format(name=name)
????'my name is andrew'