Python中类的属性及方法的总结及python改写memocache启动脚本举例

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 -*-
    # @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__
    

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 -*-
# [@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()

转载于:https://my.oschina.net/u/3804957/blog/2980683

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值