我有一个模块(实际上是单个.py文件),有一个名为HashedDir的类.
当我导入文件并实例化该类的2个实例时,当我检查对象的字段时,它们总是相同的,即使这两个对象应该是不同的.
例如:
h1 = HashedDir('/path/to/dir') print h1.getList()['files'] # /path/to/dir h2 = HashedDir('some/other/path') print h1.getList()['files'] # some/other/path print h2.getList()['files'] # some/other/path
任何的想法?
这是班级:
from os import walk from os import path from hashlib import md5 import re class HashedDir: """ A list of files with associated md5 hashes generated retrieving thou a recursive walk in the directory tree starting from a provided root directory. Also stores the dirs in each dir """ # {'files': [ # ('/path/to/file1', '52bc309e11259af15e4623c7a0abc28c'), # ('/path/to/file2', '52bc309e11259af15e4623c7a0abc28c'), # ('/path/to/dir/file3', '52bc309e11259af15e4623c7a0abc28c') # ], # 'dirs': ['/path/to/dir1', '/path/to/dir2'] # } fileList = {'files': [], 'dirs': []} ignoreList = [] def __init__(self, rootDir, ignoreList=[]): """ ignoreList is a list of regular expressions. If a file or a dir matches that regular expression, don't count it """ self.ignoreList = ignoreList for dirpath, dirnames, filenames in walk(rootDir): for fileName in filenames: completeName = path.join(dirpath,fileName) hash = md5(open(completeName).read()).hexdigest() relativePath = self._relativePath(completeName, rootDir) if not self._toBeIgnored(relativePath): self.fileList['files'].append((relativePath, hash)) for dirName in dirnames: completeName = path.join(dirpath, dirName) relativePath = self._relativePath(completeName, rootDir) if not self._toBeIgnored(relativePath): self.fileList['dirs'].append(relativePath) def _relativePath(self, path, base): return path.replace(base, '') def _toBeIgnored(self, path): for regex in self.ignoreList: if re.compile(regex).search(path) != None: return True return False def getList(self): return self.fileList
提前致谢
类中有两种变量:
类变量,在类级别定义,并且对所有实例都是通用的
实例变量,一个类的方法(通常是内定义__init__
)和合格的由实例(通常self.
).
例
class SomeClass( object ): classVariable = 0 def __init__( self ): self.instanceVariable= 0
名为的变量classVariable
是类的一部分,对所有实例都是通用的.由于的方式Python做搜索,它可以作为成员self.classVariable
,以及SomeClass.classVariable
.
名为variable的变量instanceVariable
是instance(self.
)的一部分,并且对每个实例都是唯一的.
注意.还有第三种,全球性的,但这不是你所要求的.
你在谈论文件列表吗?您将它作为类变量,使其成为您需要执行的实例变量:
self.fileList = {'files': [], 'dirs': []}
在你__ init __函数中.