我必須端口遺留代碼(〜60K LOC)從Python的2〜3中像下面兩三千結構:如何從Python的2端口`__slots__` 3
class Sample(object):
__slots__ = ('both', 'advertise')
class __metaclass__(type):
__instancecheck__ = classmethod(
lambda cls, inst: inst in ('both', 'advertise'))
both = 'both'
advertise = 'advertise'
這代碼工作正常使用Python 2,但是不與Python 3編譯並解決它,我需要將其更改爲
class Sample(object):
__slots__ = ('both', 'advertise')
class __metaclass__(type):
__instancecheck__ = classmethod(
lambda cls, inst: inst in ('both', 'advertise'))
def __init__(self):
both = 'both'
advertise = 'advertise'
什麼是處理這種變化給出一個有效的方式,它有重新上漆這麼大的文件多次?
我們必須考慮可能已經或可能不會有一個__init__
函數定義已經爲類,並且也可以嵌套類定義。
這是我到目前爲止嘗試過的。
2to3
不認爲這是一個問題,因此不會改變它。- 一種可能的方法是使用
ast
模塊修改內存中的分析樹,然後使用unparse
來回寫修改後的代碼。但這並不簡單。 - 如果沒有其他工作,我會考慮編寫一個簡單的Shell/Python腳本,它將讀取源文件,進行更改並將其寫回。
有沒有另一個快速簡單的方法來處理這種變化。
[元類已經在Python 3改變(https://docs.python.org/3/reference/datamodel.html),'__metaclass__'沒有按不再工作了。你也可以用一個共同的元類替換元類(這裏每個類都有一個),並使用'cls .__ slots __中的'lambda cls,inst:inst',這應該適用於所有類。 –
你也可以用'__init__'替換__slots__中的插槽:setattr(self,slot,slot)' –