Python類方法、靜態方法、全局變量的使用

一、全局變量

實現全局變量主要有兩種方法:聲明法和模塊法

1、聲明法

在文件開頭聲明全局變量variable,在具體函數中使用該變量時,需要事先聲明 global variable,否則系統將該變量視爲局部變量。

2、模塊法(本文主要使用模塊法)

把全局變量定義在一個單獨的模塊中,適用於不同文件之間的變量共享,而且一定程度上避免了全局變量的弊端。

二、類方法和靜態方法

Python沒有和C++中static關鍵字,它的靜態方法是怎樣的?還有其它語言中少有的類方法又是怎麼回事? 
python中實現靜態方法和類方法都是依賴於python的修飾器來實現的。

普通的對象方法、類方法和靜態方法的區別如何? 
對象方法有self參數,類方法有cls參數,靜態方法是不需要這些附加參數。

三、代碼

文件一:globalData.py

#! /usr/bin/env python
#coding=utf-8

#1、把全局變量定義在一個單獨的模塊中:

glo_resource = []


#2、類方法的使用
class Common(object):
    a1=[]

    @classmethod
    def addData(cls,a2):
        cls.a1.append(a2)

    @classmethod
    def showData(cls):
        return cls.a1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

文件二:test.py

#! /usr/bin/env python
#coding=utf-8
from globalData import * 


class MergeHost(object):

    def __init__(self, resource_list):

        self.resource_list = resource_list

    def merge_host(self):

        allResource=[]
        allResource.append(self.resource_list[0])
        for dict in self.resource_list:
            #print len(l4)
            k=0
            for item in allResource:
                #print 'item'
                if dict['host'] != item['host']:
                    k=k+1
                    #continue
                else:
                    break
                if k == len(allResource):
                    allResource.append(dict)
        taskhost=[]
        for item in allResource:
            taskhost.append(item['host'])

        print '########previous########'
        print glo_resource
        print '########previous########'
        #1、全局變量賦值
        glo_resource.append(taskhost)
        #2、類方法和類中數據成員的使用
        Common.addData(taskhost)
        print "Common list: ",Common.a1
        print "Common list: ",Common.showData()
        return taskhost

class MyClass(): 

    def method(self): 
        print("method") 

    @staticmethod 
    def staticMethod(): 
        print("static method") 

    @classmethod 
    def classMethod(cls): 
        print("class method")


class A(object):
    "This ia A Class"
    @staticmethod
    def Foo1():
        print("Call static method foo1()\n")

    @classmethod
    def Foo2(cls):
        print("Call class method foo2()")
        print("cls.__name__ is ",cls.__name__)


if __name__ == '__main__':

    resource_list=[{'host':'compute21', 'cpu':2},{'host':'compute21', 'cpu':2},{'host':'compute22', 'cpu':2},
                   {'host':'compute23', 'cpu':2},{'host':'compute22', 'cpu':2},{'host':'compute23', 'cpu':2},
                   {'host':'compute24', 'cpu':2}]

    hostSchedule = MergeHost(resource_list)
    taskhost = hostSchedule.merge_host()

    print 'glo_resource: ', glo_resource

    #print 'taskhost: '
    #print taskhost

    m = MyClass()
    m.classMethod()
    m.method()
    m.staticMethod()
    A.Foo1();
    A.Foo2();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

運行test.py得到結果:

########previous########
[]
########previous########
Common list:  [['compute21', 'compute22', 'compute23', 'compute24']]
Common list:  [['compute21', 'compute22', 'compute23', 'compute24']]
glo_resource:  [['compute21', 'compute22', 'compute23', 'compute24']]
class method
method
static method
Call static method foo1()

Call class method foo2()
('cls.__name__ is ', 'A')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章