類屬性,也是公有屬性, 類的私有屬性, 對象的共有屬性, 對象的私有屬性, 內置屬性, 函數的局部變量, 全局變量,
#/usr/bin/env python # -*- coding:utf-8 -*- class MyClass(object): var1 = '類屬性,類的公有屬性 var1' __var2 = '類的私有屬性 __var2' def func1(self): self.var3 = '對象的公有屬性 var3' self.__var4 = '對象的私有屬性 __var4' var5 = '函數的局部變量' mc = MyClass() mc.func1() #調用後才測打印出var3 print mc.var1 print mc._MyClass__var2 print mc.var3 mc1 = MyClass() # mc1.func1() #mc1沒有調用方法 print mc1.var3
經過類訪問:
#/usr/bin/env python # -*- coding:utf-8 -*- # @time :2018/1/2 21:06 # @Author :FengXiaoqing # @file :__init__.py.py # var6 = '全局變量 ' class MyClass(object): var1 = '類屬性,類的公有屬性 var1' ##定義在方法外 __var2 = '類的私有屬性 __var2' def func1(self): self.var3 = '對象的公有屬性 var3' ##定義在方法內 self.__var4 = '對象的私有屬性 __var4' var5 = '函數的局部變量' def func2(self): print self.var1 print self.__var2 print self.var3 print self.__var4 print self.var6 mc = MyClass() mc.func1() mc.func2() print '*'*50 print mc.__dict__ print MyClass.var1 #print MyClass.__var2 #不測經過類訪問 print mc.var3 #對象的屬性只能經過對象來訪問 #print MyClass.__var4 print MyClass.__dict__
公有方法python
私有方法shell
類方法memcached
- 靜態方法 - 內置方法
class MyClass(object): name = 'Test' def func1(self): print self.name, print "我是公有方法." self.__func2() #func1間接調用了func2的私有方法 def __func2(self): print self.name, print "我是私有方法." def classFun(self): print self.name, print "我是類方法." def staticFun(self): print s.name, print "我是靜態方法." mc = MyClass() mc.func1()
調用類方法:用裝飾器函數
@classmethod def classFun(self): print self.name, print "我是類方法." def staticFun(self): print s.name, print "我是靜態方法." mc = MyClass() mc.func1() MyClass.classFun()
調用靜態方法:測試
@staticmethod def staticFun(): print MyClass.name, print "我是靜態方法." mc = MyClass() mc.func1() MyClass.classFun() MyClass.staticFun()
調用內置方法:.net
class MyClass(object): name = 'Test' def __init__(self): self.func1() self.__func2() self.classFun() self.staticFun() def func1(self): print self.name, print "我是公有方法." def __func2(self): print self.name, print "我是私有方法." @classmethod def classFun(self): print self.name, print "我是類方法." @staticmethod def staticFun(): print MyClass.name, print "我是靜態方法." mc = MyClass()
測試環境:CentOS 7 Python版本: 2.7
安裝memcached:輸入下面命令安裝並用腳本測試rest
yum install -y memcached libmemcached libevent
測試方法:把腳本內容粘貼到rc.py文件中,shell下運行若是下命令:code
python rc.py start
1、rc 腳本的start、stop、restart、status方法對象
#/usr/bin/env python # -*- coding:utf-8 -*- # [@time](https://my.oschina.net/u/126678) :2018/12/06 19:11 # [@Author](https://my.oschina.net/arthor) :FengXiaoqing # [@file](https://my.oschina.net/u/726396) :rc.py import os import sys from subprocess import Popen,PIPE class Process(object): '''memcached rc script''' def __init__(self,name,program,args,workdir): self.name = name self.program = program self.args = args self.workdir = workdir def _init(self): '''/var/tmp/memcached''' if not os.path.exists(self.workdir): os.mkdir(self.workdir) os.chdir(self.workdir) def _pidFile(self): '''/var/tmp/memcached/memcached.pid''' return os.path.join(self.workdir,"%s.pid" % self.name) def _writhPid(self): if self.pid: with open(self._pidFile(),'w') as fd: fd.write(str(self.pid)) def start(self): pid = self._getPid() if pid: print "%s is already runnig..." % self.name sys.exit() self._init() cmd = self.program + ' ' + self.args p = Popen(cmd,stdout=PIPE,shell=True) self.pid = p.pid self._writhPid() print "%s start Sucessful.." % self.name def _getPid(self): p = Popen(['pidof',self.name],stdout = PIPE) pid = p.stdout.read().strip() return pid def stop(self): pid = self._getPid() if pid: os.kill(int(pid),15) if os.path.exists(self._pidFile()): os.remove(self._pidFile()) print ("%s is stopped .") % self.name def restart(self): self.stop() self.start() def status(self): pid = self._getPid() if pid: print "%s is already running..." % self.name else: print "%s is not running..." % self.name def _help(self): print ("Usage: %s {start|stop|status|restart}") % __file__ def main(): name = 'memcached' prog = '/usr/bin/memcached' args = '-u nobody -p 11211 -c 1024 -m 64' wd = '/var/tmp/memcached' pm = Process(name = name, program=prog, args=args, workdir=wd) try: cmd = sys.argv[1] except IndexError,e: print ("Option error") sys.exit() if cmd == 'start': pm.start() elif cmd == 'stop': pm.stop() elif cmd == 'restart': pm.restart() elif cmd == 'status': pm.status() else: pm._help() if __name__ == '__main__': main()