コンテンツにスキップ

利用者:Seapink/メタクラス

オブジェクト指向プログラミングにおいてメタクラスとは、インスタンスがクラスとなるクラスのことである。通常のクラスがそのインスタンスの振る舞いを定義するように、メタクラスはそのインスタンスであるクラスを、そして更にそのクラスのインスタンスの振る舞いを定義する。全てのオブジェクト指向プログラミング言語でメタクラスが利用できるわけではない。利用できるものの中でもクラスの振る舞いが定義できる範囲は様々である。各言語はそれぞれ独自のメタオブジェクトプロトコルを備えている[1]。メタオブジェクトプロトコルとは、オブジェクトとクラス、メタクラスがいかに相互作用するかの規則である。ここでいう相互作用はオブジェクト同士、クラス同士、メタクラス同士の作用も含む。

Pythonでの例[編集]

Pythonの組み込み(ビルトイン)クラス type はメタクラスである[2][3][4]。 次に示す単純なPythonのクラスについて説明する。

class Car(object):
    __slots__ = ['make', 'model', 'year', 'color']

    def __init__(self, make, model, year, color):
        self.make = make
        self.model = model
        self.year = year
        self.color = color

    @property 
    def description(self):
        """ このCarの説明を返す """
        return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

このコードを実行した時、Cartype のインスタンスになっている。上記の Car クラスのソースコードには __init__ メソッドが Car のインスタンスが生成されるたびに呼ばれるといった細々としたことは記述されていない。メタクラスが用意されていない言語ではこのような振る舞いは言語仕様で定義されており、変更することは不可能である。Pythonではメタクラス type がこれらの動作を決定しており、 type の代わりに違うメタクラスを使用することでこれらの振る舞いを変更することが可能である。

上に示した例は4つの属性 makemodelyearcolor の辺りが冗長である。メタクラスを使えば、この冗長さを取り除くことが可能である。Pythonではメタクラスは type のサブクラスとして定義するのが最も簡単である。

 class AttributeInitType(type):
     def __call__(self, *args, **kwargs):
         """ 新しいインスタンスを生成する """
 
         # 普通にインスタンスを生成する
         obj = type.__call__(self, *args)
 
         # 生成したインスタンスの属性を設定する
         for name in kwargs:
             setattr(obj, name, kwargs[name])
 
         # 生成したインスタンスを返す
         return obj

このメタクラスはインスタンスの生成を上書きしているだけで、他の機能はまだ type が処理している。

さて、このメタクラスを用いて Car クラスを書き直すことが可能である。Python 2ではクラス定義中で __metaclass__ にこのメタクラスを代入すればよい(Python 3では代わりに metaclass=M と名前付き引数として与える)。

 class Car(object):
     __metaclass__ = AttributeInitType
     __slots__ = ['make', 'model', 'year', 'color']
 
     @property
     def description(self):
         """ Return a description of this car. """
         return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

これで、 Car のインスタンスを次のように生成できる。

 cars = [
     Car(make='Toyota', model='Prius', year=2005, color='green'),
     Car(make='Ford', model='Prefect', year=1979, color='blue')]

In Smalltalk-80[編集]

The Smalltalk-80 metaclass hierarchy as a UML diagram

In Smalltalk, everything is an object. Additionally, Smalltalk is a class based system, which means that every object has a class that defines the structure of that object (i.e. the instance variables the object has) and the messages an object understands. Together this implies that a class in Smalltalk is an object and that therefore a class needs to be an instance of a class (called metaclass).

As an example, the a car object c is an instance of the class Car. In turn, the class Car is again an object and as such an instance of the metaclass of Car called Car class. Note the blank in the name of the metaclass. The name of the metaclass is the Smalltalk expression that, when evaluated, results in the metaclass object. Thus evaluating Car class results in the metaclass object for Car whose name is Car class (one can confirm this by evaluating Car class name which returns the name of the metaclass of Car.)

Class methods actually belong to the metaclass, just as instance methods actually belong to the class. When a message is sent to the object 2, the search for the method starts in Integer. If it is not found it proceeds up the superclass chain, stopping at Object whether it is found or not.

When a message is sent to Integer the search for the method starts in Integer class and proceeds up the superclass chain to Object class. Note that, so far, the metaclass inheritance chain exactly follows that of the class inheritance chain. But the metaclass chain extends further because Object class is the subclass of Class. All metaclasses are subclasses of Class.

In early Smalltalks, there was only one metaclass called Class. This implied that the methods all classes have were the same, in particular the method to create new objects, i.e., new. To allow classes to have their own methods and their own instance variables (called class instance variables and should not be confused with class variables), Smalltalk-80 introduced for each class C their own metaclass C class.

Since there is no requirement that metaclasses behave differently from each other, all metaclasses are instances of only one class called Metaclass. The metaclass of Metaclass is called Metaclass class which again is an instance of class Metaclass.

In Smalltalk-80, every class (except Object) has a superclass. The abstract superclass of all metaclasses is Class, which describes the general nature of classes.

The superclass hierarchy for metaclasses parallels that for classes, except for class Object. ALL metaclasses are subclasses of Class, therefore:

  • Object class superclass == Class.

Like conjoined twins, classes and metaclasses are born together. Metaclass has an instance variable thisClass, which points to its conjoined class. Note that the usual Smalltalk class browser does not show metaclasses as separate classes. Instead the class browser allows to edit the class together with its metaclass at the same time.

The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance:

  • Object is the base class that provides common methods for all objects; "an object" is an integer, or a widget, or a Car, etc.
  • Class is the base metaclass that provides common methods for all classes; "a class" is something like Integer, or Widget, or Car, etc.
  • Metaclass has the same relation to "a Metaclass".

Four classes provide the facilities to describe new classes. Their inheritance hierarchy (from Object), and the main facilities they provide are:

Object - default behavior common to all objects, like class access
Behavior - minimum state for compiling methods and creating/running objects
ClassDescription (abstract class) - class/variable naming, comments
Class - similar, more comprehensive, facilities to superclasses
Metaclass - initializing class variables, instance creation messages

利用可能な言語とツール[編集]

有名なプログラミング言語でメタクラスが利用可能なものは以下である。

以下のようにそれほど普及していない言語の中にはメタクラスが利用できるものもある。いくつかは研究目的であり、1990年代初頭にまで遡る[5]: OpenJavaOpenC++OpenAdaCorbaScriptObjVLispObject-ZMODEL-KXOTclMELDCなど。

また、Prologのオブジェクト指向拡張であるLogtalkでもメタクラスが利用できる。

さらに、Resource Description Framework (RDF)とUnified Modeling Language (UML)は両者共にメタクラスが利用できる。

関連項目[編集]

出典[編集]

  1. ^ Ira R. Forman and Scott Danforth (1999). Putting Metaclasses to Work. ISBN 0-201-43305-2 
  2. ^ IBM Metaclass programming in Python, parts 1, 2 and 3
  3. ^ Artima Forum: Metaclasses in Python 3.0 (part 1 of 2) (part 2 of 2)
  4. ^ David Mertz. “A Primer on Python Metaclass Programming”. ONLamp. 2006年6月28日閲覧。
  5. ^ An implementation of mixins in Java using metaclasses