В Пайтън type значи няколко неща
Класовете са просто инстанции на type.
type(name, bases, dict)
Foo = type('Foo', (A, B, C), {'x':1, 'y':2})
Долното е синтактична захар за горното...
class Foo(A, B, C):
x = 1
y = 2
class metacls(type):
def __new__(mcs, name, bases, dict):
dict['foo'] = 'metacls was here'
return type.__new__(mcs, name, bases, dict)
Foo = metacls('Foo', (A, B, C), {'x':1, 'y':2})
type(Foo) # metacls
class Foo(A, B, C, metaclass=metacls):
x = 1
y = 2
[Metaclasses] are deeper magic than 99% of the users should ever worry about. If you wonder whether you need them, you don't (the people who actually need them know with certainty that they need them, and don't need an explanation about why).
— Tim Peters
class MyDict(dict):
pass
class Foo(type):
@classmethod
def __prepare__(self, name, bases):
return MyDict()
def foo(x: int, y: string):
pass
Кодът, който разглеждахме през първия час можете да намерите тук:
http://pythonmulti.codeplex.com/releases/view/43090
Кодът, който разглеждахме през втория час можете да намерите тук:
http://pythondbc.codeplex.com/releases/view/42938