您好,登錄后才能下訂單哦!
1.類的屬性總結
類屬性,也是公有屬性,
類的私有屬性,
對象的共有屬性,
對象的私有屬性,
內置屬性,
函數的局部變量,
全局變量,
#/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 -*-
# @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__
2.類的方法總結
公有方法
私有方法
類方法
靜態方法
內置方法
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()
調用內置方法:
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()
3.Python編寫memcached啟動腳本
測試環境:CentOS 7
Python版本: 2.7
安裝memcached:輸入下面命令安裝并用腳本測試
yum install -y memcached libmemcached libevent
測試方法:把腳本內容粘貼到rc.py文件中,shell下運行如果下命令:
python rc.py start
一、rc 腳本的start、stop、restart、status方法
#/usr/bin/env python
# -*- coding:utf-8 -*-
# [@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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。