Python中的类方法及属性总结举例,编写memcached启动脚本举例

1.类的属性总结
类属性,也是公有属性,

成都创新互联公司成立与2013年,是专业互联网技术服务公司,拥有项目成都网站制作、网站建设、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元文山州做网站,已为上家服务,为文山州各地企业和个人服务,联系电话:18982081108

类的私有属性,

对象的共有属性,

对象的私有属性,

内置属性,

函数的局部变量,

全局变量,
#/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()

网页题目:Python中的类方法及属性总结举例,编写memcached启动脚本举例
标题链接:http://ybzwz.com/article/ggdigd.html