Information about 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.
Even among object-oriented programming languages, not all languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior depends on the language. Each language has its own metaobject protocol, a set of rules that govern how objects, classes, and metaclasses interact.
At run time,
The above example contains some redundant code to do with the four attributes
class AttributeInitType(type): def __call__(self, *args, **kwargs): """ Create a new instance. """
This metaclass only overrides object creation. All other aspects of class and object behavior are still handled by
Now the class
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)
cars = [ Car(make='Toyota', model='Prius', year=2005, color='green'), Car(make='Ford', model='Prefect', year=1979, color='blue')]
Metaclass programming can be confusing, and it is rare in real-world Python code.
In early Smalltalks, there was one metaclass called
In Smalltalk-80, a class is an instance of its own metaclass; and each class can have unique methods for creating objects. Metaclasses, like other classes, contain methods used by their instances. But metaclasses are all instances of one class, called
Names are not given to metaclasses. The metaclass of class
The methods of a metaclass create instances, and initialize class variables.
In Smalltalk-80, every class (except
The superclass hierarchy for metaclasses parallels that for classes, except for class
The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance:
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
Aside - another way of saying "metaclass of
When a message is sent to
All metaclasses are instances of class
Logtalk, an object-oriented extension of Prolog, also supports metaclasses.
Resource Description Framework (RDF) and Unified Modeling Language (UML) both support metaclasses.
..... Click the link for more information.
Even among object-oriented programming languages, not all languages support metaclasses. Among those that do, the extent to which metaclasses can override any given aspect of class behavior depends on the language. 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 classtype is a metaclass. Consider this simple Python class:
> 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): """ 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 a type object. 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.
- Additionally, set attributes on the new object.
- Return the new object.
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 by assigning to __metaclass__ within the class definition:
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 objects can then be instantiated like this:
cars = [ Car(make='Toyota', model='Prius', year=2005, color='green'), Car(make='Ford', model='Prefect', year=1979, color='blue')]
Metaclass programming can be confusing, and it is rare in real-world Python code.
In Smalltalk-80
In Smalltalk, everything is an object. There are two kinds of objects: those that can create instances of themselves (classes), and others that cannot. Every object is the instance of a class. Every class is the instance of a metaclass.In early Smalltalks, there was one metaclass called
Class. The object creation method of all classes was the same, i.e., new. A class sent the message new could only return an object with uninitialized instance variables. Smalltalk's designers wanted to send one message to an object to initiate both creation and initializaton. They achieved this in Smalltalk-80.
In Smalltalk-80, a class is an instance of its own metaclass; and each class can have unique methods for creating objects. Metaclasses, like other classes, contain methods used by their instances. But metaclasses are all instances of one class, called
Metaclass. Unlike classes, metaclasses do not need flexibile creation methods, because classes all have the same structure. For instance, the class Car has instance variables just like any other class. People using (and not re-designing) Smalltalk do not need to write class creation methods.
Names are not given to metaclasses. The metaclass of class
Sphere is simply referred to as "the metaclass of class Sphere". The metaclass of a class may be accessed by sending the message class to the class.
The methods of a metaclass create instances, and initialize class variables.
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.
Metaclass has an instance variable thisClass, which points to its conjoined class.
The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance:
Objectis the base class that provides common methods for all objects; "an object" is an integer, or a widget, or aCar, etc.Classis the base metaclass that provides common methods for all classes; "a class" is something likeInteger, orWidget, orCar, etc.Metaclasshas the same relation to "a Metaclass".
- Object - default behavior common to all objects, like class access
- ::ClassDescription (abstract class) - class/variable naming, comments
- :::Class - similar, more comprehensive, facilities to superclasses
- :::Metaclass - initializing class variables, instance creation messages + read on...
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 not found it proceeds up the superclass chain, stopping at Object whether it is found or not.
Aside - another way of saying "metaclass of
Integer" is Integer class.
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.
All metaclasses are instances of class
Metaclass. So the metaclass of Metaclass is an instance of Metaclass.
Support in languages and tools
The following programming languages support metaclasses.- Common Lisp, via CLOS
- Groovy
- Object Pascal (especially in Borland Delphi)
- Python
- Perl, via the metaclass pragma
- Smalltalk
Logtalk, an object-oriented extension of Prolog, also supports metaclasses.
Resource Description Framework (RDF) and Unified Modeling Language (UML) both support metaclasses.
See also
External links
- Citations from CiteSeer
- Description from the Free On-Line Dictionary of Computing
- David Mertz. A Primer on Python Metaclass Programming. ONLamp. Retrieved on June 28, 2006.
Further reading
- Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), ISBN 0-201-43305-2
Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. It is based on several techniques, including inheritance, modularity, polymorphism, and encapsulation.
..... Click the link for more information.
..... Click the link for more information.
Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. The source code is written in a programming language.
..... Click the link for more information.
..... Click the link for more information.
This article or section may be confusing or unclear for some readers.
Please [improve the article] or discuss this issue on the talk page. This article has been tagged since December 2006.
..... Click the link for more information.
Please [improve the article] or discuss this issue on the talk page. This article has been tagged since December 2006.
..... Click the link for more information.
A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. Programming languages, like natural languagess, are defined by syntactic and semantic rules which describe their structure and meaning respectively.
..... Click the link for more information.
..... Click the link for more information.
In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other objects. The object that the metaobject is about is called the base object.
..... Click the link for more information.
..... Click the link for more information.
Python
Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
Smalltalk
Paradigm: object-oriented
Appeared in: Development started in 1969
Publicly available in 1980
Designed by: Alan Kay, Dan Ingalls, Adele Goldberg
Developer: Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and Xerox PARC
..... Click the link for more information.
Paradigm: object-oriented
Appeared in: Development started in 1969
Publicly available in 1980
Designed by: Alan Kay, Dan Ingalls, Adele Goldberg
Developer: Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and Xerox PARC
..... Click the link for more information.
Object may refer to:
..... Click the link for more information.
- Object (philosophy), a thing, being or concept
- Physical entity, something that is tangible and within the grasp of the senses
..... Click the link for more information.
Method may refer to:
..... Click the link for more information.
- Scientific method, a series of steps taken to acquire knowledge
- Method (computer science), a piece of code associated with a class or object to perform a task
..... Click the link for more information.
In computer science, data that has several parts can be divided into fields. For example, a computer may represent today's date as three distinct fields: the day, the month and the year.
..... Click the link for more information.
..... Click the link for more information.
variable (IPA pronunciation: [ˈvæɹiəbl]) (sometimes called a pronumeral) is a symbolic representation denoting a quantity or expression.
..... Click the link for more information.
..... Click the link for more information.
Superclass may be:
..... Click the link for more information.
- Superclass (biology), a taxonomic rank intermediate between subphylum and class.
- Superclass (computer science), a class from which other classes are derived
- Superclass (database theory),
..... Click the link for more information.
Conjoined twins
Classification & external resources
ICD-10 Q 89.4
ICD-9 759.4
..... Click the link for more information.
Classification & external resources
ICD-10 Q 89.4
ICD-9 759.4
"Siamese twins" redirects here. For other senses of that term, see Siamese twins (disambiguation).
..... Click the link for more information.
In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived classes, take over (or inherit
..... Click the link for more information.
..... Click the link for more information.
A state is a political association with effective dominion over a geographic area. It usually includes the set of institutions that claim the authority to make the rules that govern the people of the society in that territory, though its status as a state often depends in part on
..... Click the link for more information.
..... Click the link for more information.
compiler is a computer program (or set of programs) that translates text written in a computer language (the source language) into another computer language (the target language).
..... Click the link for more information.
..... Click the link for more information.
abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declared subtype.
..... Click the link for more information.
..... Click the link for more information.
confusing or unclear for some readers.
Please help [ improve the article] or discuss these issues on the talk page. In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods
..... Click the link for more information.
Please help [ improve the article] or discuss these issues on the talk page. In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods
..... Click the link for more information.
A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. Programming languages, like natural languagess, are defined by syntactic and semantic rules which describe their structure and meaning respectively.
..... Click the link for more information.
..... Click the link for more information.
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard X3.226-1994. Developed to standardize the divergent variants of Lisp which predated it, it is not an implementation but rather a language specification.
..... Click the link for more information.
..... Click the link for more information.
- ''
..... Click the link for more information.
Groovy
Paradigm: object-oriented, scripting
Appeared in: 2003
Designed by: JCP
Typing discipline: dynamic, strong
Influenced by: Python, Ruby, Perl, Smalltalk, Java
Groovy
..... Click the link for more information.
Paradigm: object-oriented, scripting
Appeared in: 2003
Designed by: JCP
Typing discipline: dynamic, strong
Influenced by: Python, Ruby, Perl, Smalltalk, Java
Groovy
..... Click the link for more information.
Object Pascal is an object oriented derivative of Pascal mostly known as the primary programming language of Borland Delphi. It is also known as the Delphi programming language when describing the dialect used by Borland Delphi.
..... Click the link for more information.
..... Click the link for more information.
Borland Delphi (now a product of Borland's subsidiary, CodeGear) is a software development package created by Borland. The 11th and latest version, Delphi 2007 supports the Delphi programming language (Object Pascal) and C++ for the 32 bit Microsoft Windows platform, and Delphi and
..... Click the link for more information.
..... Click the link for more information.
Python
Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
Paradigm: Multi-paradigm
Appeared in: 1991
Designed by: Guido van Rossum
Developer: Python Software Foundation
Latest release: 2.5.1/ April 18 2007
Latest unstable release: 3.
..... Click the link for more information.
Perl
Paradigm: Multi-paradigm
Appeared in: 1987
Designed by: Larry Wall
Latest release: 5.8.8/ January 31 2006
Typing discipline: Dynamic
Influenced by: AWK, BASIC, BASIC-PLUS, C, C++, Lisp, Pascal, Python, sed, Unix shell
..... Click the link for more information.
Paradigm: Multi-paradigm
Appeared in: 1987
Designed by: Larry Wall
Latest release: 5.8.8/ January 31 2006
Typing discipline: Dynamic
Influenced by: AWK, BASIC, BASIC-PLUS, C, C++, Lisp, Pascal, Python, sed, Unix shell
..... Click the link for more information.
Smalltalk
Paradigm: object-oriented
Appeared in: Development started in 1969
Publicly available in 1980
Designed by: Alan Kay, Dan Ingalls, Adele Goldberg
Developer: Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and Xerox PARC
..... Click the link for more information.
Paradigm: object-oriented
Appeared in: Development started in 1969
Publicly available in 1980
Designed by: Alan Kay, Dan Ingalls, Adele Goldberg
Developer: Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and Xerox PARC
..... Click the link for more information.
OpenJava is a programming tool that parses and analyzes Java source code. It uses a metaobject protocol (MOP) to provide services for language extensions.
This isn't to be confused with OpenJDK, which is the open source release of the Java compiler runtime and tools.
..... Click the link for more information.
This isn't to be confused with OpenJDK, which is the open source release of the Java compiler runtime and tools.
..... Click the link for more information.
CorbaScript is an object-oriented scripting language.
..... Click the link for more information.
See also
- CORBA
- CorbaWeb
External links
- The CorbaScript Language
- CORBA Scripting joint revised submission
- IDLscript December 2000 RTF Updated Specification
..... Click the link for more information.
ObjVlisp is a 1984 object-oriented extension of Vlisp with a Reflective architecture.
["Metaclasses are First Class: The ObjVlisp Model", P. Cointe, SIGPLAN Notices 22(121):156-167 (Dec 1987) (OOPSLA '87)].
..... Click the link for more information.
["Metaclasses are First Class: The ObjVlisp Model", P. Cointe, SIGPLAN Notices 22(121):156-167 (Dec 1987) (OOPSLA '87)].
..... Click the link for more information.
This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus