Define a This class, Calc This class method implements the sum of square element squares:
#file_name: Init.py#!/usr/bin/env pythonclass A (): Def __init__ (Self,args): Self.args = args def calc (self): sum = 0 for i in Self.args:sum + = i * I print sum if __name__ = = ' __main__ ': a = A ([A.calc]) #实例化 () #类方法
Execute script init.py:
0> python init.py14
Perform different functions by judging whether there are parameter inputs:
Class a (): def __init__ (Self,args): self.args = args def cal (self): sum = 0 for n in self.args: sum += n * n print sum def get_options (): usage = "usage: %prog [options]" optionparser = optparse. Optionparser parser = optionparser (usage) parser.add_ Option ("-D", "--date", action= "store", type= "string", dest= "Date", help= "Date you want request history") &nbSp; options,args = parser.parse_args () date = options.date print dateif __name__ == ' __main__ ': if len (SYS.ARGV) > 1: get_options () else: a = a ([i]) a.cal ()
Separate functions are called when there are parameter inputs and no parameter inputs:
0> python2.6 parser.py-d 2015-09-11 2015-09-110> python2.6 parser.py 14
Python other functions call __init__ initialized values