我試圖引入python 3對包mime的支持,代碼正在做一些我從未見過的事情。靜態類在導入時被初始化。 python 2如何初始化導入的靜態類
有一個類Types()
在包中用作靜態類。
class Types(with_metaclass(ItemMeta, object)): # I changed this for 2-3 compatibility
type_variants = defaultdict(list)
extension_index = defaultdict(list)
# __metaclass__ = ItemMeta # unnessecary now
def __init__(self, data_version=None):
self.data_version = data_version
的type_variants
defaultdict是什麼是越來越充滿在Python 2而不是在3
它非常似乎被這個類是被稱爲mime_types.py
不同的文件時得到填補。
class MIMETypes(object):
_types = Types(VERSION)
def __repr__(self):
return '<MIMETypes version:%s>' % VERSION
@classmethod
def load_from_file(cls, type_file):
data = open(type_file).read()
data = data.split('\n')
mime_types = Types()
for index, line in enumerate(data):
item = line.strip()
if not item:
continue
try:
ret = TEXT_FORMAT_RE.match(item).groups()
except Exception as e:
__parsing_error(type_file, index, line, e)
(unregistered, obsolete, platform, mediatype, subtype, extensions,
encoding, urls, docs, comment) = ret
if mediatype is None:
if comment is None:
__parsing_error(type_file, index, line, RuntimeError)
continue
extensions = extensions and extensions.split(',') or []
urls = urls and urls.split(',') or []
mime_type = Type('%s/%s' % (mediatype, subtype))
mime_type.extensions = extensions
...
mime_type.url = urls
mime_types.add(mime_type) # instance of Type() is being filled?
return mime_types
功能startup()
正在運行時mime_types.py
是進口的,它做到這一點。
def startup():
global STARTUP
if STARTUP:
type_files = glob(join(DIR, 'types', '*'))
type_files.sort()
for type_file in type_files:
MIMETypes.load_from_file(type_file) # class method is filling Types?
STARTUP = False
這一切對我來說都很奇怪。 MIMETypes
類首先在第一行創建一個Types()
的實例。 _types = Types(VERSION)
。然後它似乎什麼也不做,只使用load_from_file()
類方法中創建的mime_types
實例。 mime_types = Types()
。
這類事隱約讓我想起了javascript類的構建。實例mime_types
如何填充Types.type_variants
,以便當它像這樣導入時。
from mime import Type, Types
類的type_variants
defaultdict都可以使用。爲什麼不在python 3中工作?
編輯:
添加額外的代碼顯示type_variants
如何填充
(In "Types" Class)
@classmethod
def add_type_variant(cls, mime_type):
cls.type_veriants[mime_type.simplified].append(mime_type)
@classmethod
def add(cls, *types):
for mime_type in types:
if isinstance(mime_type, Types):
cls.add(*mime_type.defined_types())
else:
mts = cls.type_veriants.get(mime_type.simplified)
if mts and mime_type in mts:
Warning('Type %s already registered as a variant of %s.',
mime_type, mime_type.simplified)
cls.add_type_variant(mime_type)
cls.index_extensions(mime_type)
你可以看到MIMETypes
使用add()
類方法。
你給出的代碼包含一個名爲除了正本報關'type_variants'任何變量的引用。那麼它怎麼可能在此代碼中以任何方式被修改或「填充」或訪問?這個問題適用於任何Python版本。在Python2的情況下,它必須在其他地方填充。您的列表是否遺漏了重要的東西? –
@PaulCornelius:是的,我忽略了與'type_variants'字典交互的類方法。 'MIMETypes'具有調用'add()'來填充字典的方法。 – thundergolfer
你是什麼意思的「靜態類」?這可能意味着其他語言中的某些內容(含義取決於語言),但在Python中,該形容詞不能應用於該名詞。 – user2357112