SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
メタクラスプロトコル

   aodag
クラスとオブジェクト

>>> class A(object):
... pass
...
>>> A
<class '__main__.A'>
>>> a = A()
>>> AA = A
>>> aa = AA()
>>> a
<__main__.A object at 0x922b0>
>>> aa
<__main__.A object at 0x92210>
>>> a.__class__
<class '__main__.A'>
>>> a.__class__ is A
True
>>> aa.__class__ is A
True
クラスとタイプ

>>> aa.__class__ is A
True
>>> A.__class__
<type 'type'>
typeコンストラクタで動的にクラス定義

>>> X = type('X', (A,), {})
>>> X
<class '__main__.X'>
>>> x = X()
>>> isinstance(x, A)
True
>>> isinstance(x, X)
True
クラスとメタクラス

>>> class AType(type):
... def __init__(cls, name, parents, dct):
...   super(AType, cls).__init__(name, parents, dct)
...   print '%s defined' % name
...
>>> class AAA(object):
... __metaclass__ = AType
...
AAA defined
メタクラス応用

def trace(func):
  def wrap(*args, **kwargs):
     print args, kwargs
     return func(*args, **kwargs)
  return wrap

class Traceable(type):
  def __init__(cls, name, parents, dct):
     super(Traceable, cls).__init__(name, parents, dct)
     for k, v in [(k, trace(v)) for k, v in dct.iteritems() if callable
(v) and not k.startswith('_')]:
       setattr(cls, k, v)
使ってみる

class Hello(object):
  __metaclass__ = Traceable
  def greedy(self, name):
     print "Hello, %s" % name

h = Hello()
h.greedy('aodag')

Más contenido relacionado

Más de Atsushi Odagiri

パッケージングの今
パッケージングの今パッケージングの今
パッケージングの今
Atsushi Odagiri
 
パッケージングの今と未来
パッケージングの今と未来パッケージングの今と未来
パッケージングの今と未来
Atsushi Odagiri
 
パッケージングの今と未来
パッケージングの今と未来パッケージングの今と未来
パッケージングの今と未来
Atsushi Odagiri
 
Pyconjp2012 memory-of-europython
Pyconjp2012 memory-of-europythonPyconjp2012 memory-of-europython
Pyconjp2012 memory-of-europython
Atsushi Odagiri
 
What makes pyramid unique
What makes pyramid uniqueWhat makes pyramid unique
What makes pyramid unique
Atsushi Odagiri
 
エキPy lt repoze.whoの紹介
エキPy lt repoze.whoの紹介エキPy lt repoze.whoの紹介
エキPy lt repoze.whoの紹介
Atsushi Odagiri
 
BPStudy#54 そろそろPython3
BPStudy#54 そろそろPython3BPStudy#54 そろそろPython3
BPStudy#54 そろそろPython3
Atsushi Odagiri
 

Más de Atsushi Odagiri (20)

みんなのPython勉強会#77 パッケージングしよう
みんなのPython勉強会#77 パッケージングしようみんなのPython勉強会#77 パッケージングしよう
みんなのPython勉強会#77 パッケージングしよう
 
async/await の向こう側 PyCon Kyushu 2022
async/await の向こう側 PyCon Kyushu 2022async/await の向こう側 PyCon Kyushu 2022
async/await の向こう側 PyCon Kyushu 2022
 
パッケージングの呼び声 Python Charity Talks in Japan 2021.02
パッケージングの呼び声 Python Charity Talks in Japan 2021.02パッケージングの呼び声 Python Charity Talks in Japan 2021.02
パッケージングの呼び声 Python Charity Talks in Japan 2021.02
 
eggとはなんだったのか 栄光のsetuptools
eggとはなんだったのか 栄光のsetuptoolseggとはなんだったのか 栄光のsetuptools
eggとはなんだったのか 栄光のsetuptools
 
pyconjp 2019 LT 今日のsetuptools
pyconjp 2019 LT 今日のsetuptoolspyconjp 2019 LT 今日のsetuptools
pyconjp 2019 LT 今日のsetuptools
 
Pythonでの開発を効率的に進めるためのツール設定
Pythonでの開発を効率的に進めるためのツール設定Pythonでの開発を効率的に進めるためのツール設定
Pythonでの開発を効率的に進めるためのツール設定
 
Pythonとパッケージングと私
Pythonとパッケージングと私Pythonとパッケージングと私
Pythonとパッケージングと私
 
Python3 移行への軌跡
Python3 移行への軌跡Python3 移行への軌跡
Python3 移行への軌跡
 
パッケージングを支える技術 pyconjp2016
パッケージングを支える技術 pyconjp2016パッケージングを支える技術 pyconjp2016
パッケージングを支える技術 pyconjp2016
 
Pyramid入門
Pyramid入門Pyramid入門
Pyramid入門
 
パッケージングの今
パッケージングの今パッケージングの今
パッケージングの今
 
パッケージングの今と未来
パッケージングの今と未来パッケージングの今と未来
パッケージングの今と未来
 
パッケージングの今と未来
パッケージングの今と未来パッケージングの今と未来
パッケージングの今と未来
 
Bplt11 form alchemy
Bplt11 form alchemyBplt11 form alchemy
Bplt11 form alchemy
 
Python3でwebアプリ
Python3でwebアプリPython3でwebアプリ
Python3でwebアプリ
 
Pyconjp2012 memory-of-europython
Pyconjp2012 memory-of-europythonPyconjp2012 memory-of-europython
Pyconjp2012 memory-of-europython
 
What makes pyramid unique
What makes pyramid uniqueWhat makes pyramid unique
What makes pyramid unique
 
エキPy lt repoze.whoの紹介
エキPy lt repoze.whoの紹介エキPy lt repoze.whoの紹介
エキPy lt repoze.whoの紹介
 
World plonedaylt
World plonedayltWorld plonedaylt
World plonedaylt
 
BPStudy#54 そろそろPython3
BPStudy#54 そろそろPython3BPStudy#54 そろそろPython3
BPStudy#54 そろそろPython3
 

メタクラスプロトコル