53
A.42.M
ETACLASSES
InPython2,youcouldcreatemetaclasseseitherby definingthe
metaclass
argumentintheclassdeclaration,
orbydefiningaspecialclass-level
__metaclass__
attribute.InPython3,theclass-levelattributehas been
eliminated.
Notes Python2
Python3
①
class C(metaclass=PapayaMeta):
pass
unchanged
②
class Whip:
__metaclass__ = PapayaMeta
class Whip(metaclass=PapayaMeta):
pass
③
class C(Whipper, Beater):
__metaclass__ = PapayaMeta
class C(Whipper, Beater, metaclass=PapayaMeta):
pass
1. Declaringthemetaclass s intheclassdeclarationworkedinPython2,and itstillworksthesameinPython3.
2. Declaringthemetaclass s inaclassattributeworkedinPython2,butdoesn’tworkinPython3.
3. The
2to3
scriptissmartenoughtoconstructavalidclassdeclaration,eveniftheclassisinheritedfromone
ormorebaseclasses.
A.43.M
ATTERSOFSTYLE
Therestofthe“fixes”listedherearen’treally fixesperse.Thatis,thethingstheychangearematters of
style,notsubstance.TheyworkjustaswellinPython3astheydoinPython2,butthedevelopersof
PythonhaveavestedinterestinmakingPythoncodeas uniformaspossible.Tothatend,thereisan
official
Pythonstyleguidewhichoutlines—inexcruciatingdetail—allsortsofnitpickydetails thatyoualmost
certainlydon’tcareabout.Andgiventhat
2to3
providessuchagreatinfrastructureforconvertingPython
codefromonethingtoanother,theauthorstookituponthemselvestoaddafewoptionalfeaturesto
improvethereadabilityofyourPythonprograms.
A.43.1.set()
LITERALS
(
EXPLICIT
)
InPython2,theonlywaytodefinealiteralsetinyourcodewastocall
set(a_sequence)
.Thisstillworksin
Python3,butaclearerwayofdoingitistousethenewsetliteralnotation:curlybraces.Thisworks for
464