翻訳と辞書
Words near each other
・ "O" Is for Outlaw
・ "O"-Jung.Ban.Hap.
・ "Ode-to-Napoleon" hexachord
・ "Oh Yeah!" Live
・ "Our Contemporary" regional art exhibition (Leningrad, 1975)
・ "P" Is for Peril
・ "Pimpernel" Smith
・ "Polish death camp" controversy
・ "Pro knigi" ("About books")
・ "Prosopa" Greek Television Awards
・ "Pussy Cats" Starring the Walkmen
・ "Q" Is for Quarry
・ "R" Is for Ricochet
・ "R" The King (2016 film)
・ "Rags" Ragland
・ ! (album)
・ ! (disambiguation)
・ !!
・ !!!
・ !!! (album)
・ !!Destroy-Oh-Boy!!
・ !Action Pact!
・ !Arriba! La Pachanga
・ !Hero
・ !Hero (album)
・ !Kung language
・ !Oka Tokat
・ !PAUS3
・ !T.O.O.H.!
・ !Women Art Revolution


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

metaclass : ウィキペディア英語版
metaclass

In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior varies. Metaclasses can be implemented by having classes be first-class citizen, in which case a metaclass is simply an object that constructs classes. Each language has its own metaobject protocol, a set of rules that govern how objects, classes, and metaclasses interact.
==Python example==
In Python, the builtin class type is a metaclass.〔IBM Metaclass programming in Python, parts (1 ), (2 ) and (3 )〕〔Artima Forum: Metaclasses in Python 3.0 ((part 1 of 2) ) ((part 2 of 2) )〕 Consider this simple Python class:

class Car(object):
def __init__(self, make, model, year, color):
self.make = make
self.model = model
self.year = year
self.color = color
@property
def description(self):
""" Return a description of this car. """
return "%s %s %s %s" % (self.color, self.year, self.make, self.model)

At run time, Car itself is an instance of type. The source code of the Car class, shown above, does not include such details as the size in bytes of Car objects, their binary layout in memory, how they are allocated, that the __init__ method is automatically called each time a Car is created, and so on. These details come into play not only when a new Car object is created, but also each time any attribute of a Car is accessed. In languages without metaclasses, these details are defined by the language specification and can't be overridden. In Python, the metaclass - type - controls these details of Car's behavior. They can be overridden by using a different metaclass instead of type.
The above example contains some redundant code to do with the four attributes make, model, year, and color. It is possible to eliminate some of this redundancy using a metaclass. In Python, a metaclass is most easily defined as a subclass of type.

class AttributeInitType(type):
def __call__(self,
*args,
*
*kwargs):
""" Create a new instance. """
# First, create the object in the normal default way.
obj = type.__call__(self,
*args)
# Additionally, set attributes on the new object.
for name, value in kwargs.items():
setattr(obj, name, value)
# Return the new object.
return obj

This metaclass only overrides object creation. All other aspects of class and object behavior are still handled by type.
Now the class Car can be rewritten to use this metaclass. This is done in Python 2 by assigning to __metaclass__ within the class definition:

class Car(object):
__metaclass__ = AttributeInitType
@property
def description(self):
""" Return a description of this car. """
return " ".join(str(getattr(self, attr, "Unknown"))
for attr in self.__slots__)

In Python 3 you provide a named argument, metaclass=M to the class definition instead:

class Car(object, metaclass=AttributeInitType):
@property
def description(self):
""" Return a description of this car. """
return " ".join(str(getattr(self, attr, "Unknown"))
for attr in self.__slots__)

Car objects can then be instantiated like this:

new_car = Car(make='Toyota', model='Prius', year=2005, color='Green')
old_car = Car(make='Ford', model='Prefect', year=1979)


抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「metaclass」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.