类属性,也是公有属性,
类的私有属性,对象的共有属性,对象的私有属性,内置属性,函数的局部变量,全局变量,
#/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() #调用后才测打印出var3print mc.var1print mc._MyClass__var2print mc.var3mc1 = 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 "我是静态方法."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.pyimport osimport sysfrom subprocess import Popen,PIPEclass 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()总结
以上是内存溢出为你收集整理的Python中的类方法及属性总结举例,编写memcached启动脚本举例全部内容,希望文章能够帮你解决Python中的类方法及属性总结举例,编写memcached启动脚本举例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)