KBD

Keith Devens .com

Saturday, May 17, 2008 Flag waving
We ought always to deal justly, not only with those who are just to us, but likewise to those who... – Hierocles
← Spider solitaireMetaC →

Daily link icon Wednesday, December 14, 2005

Python's metaclasses

Dev Shed has an excellent introduction to Python's metaclasses (via DPU). I get it now:

  • Python's metaclasses are just normal classes that inherit from type
  • When you instantiate a metaclass the object returned is itself a class
  • To declare that a class has a given metaclass, set the __metaclass__ data member to the metaclass when declaring the class.
  • The metaclass' __init__ method is called when a class that has it as its metaclass is declared (__init__ for metaclasses takes (klass, klass_name, base_classes, dict_of_attributes) as its parameters).

And that's it (according to the article). So, it seems that Python very nearly has a prototype-based object system, but not quite. (And I ❤ prototype-based object systems.) With a prototype-based object system the distinction between metaclasses and classes (heck, and objects) goes away, so it seems to me that you could do the same thing as what Python calls metaclasses simply by overriding a particular object's clone method. Am I wrong?

← Spider solitaireMetaC →

Comments XML gif

DJ Hannibal wrote:

"Chapter 20. Descriptors, Decorators, and Metaclasses" in "Python Cookbook, 2nd Edition" is really good. (It is on my Safari bookshelf if you want to take a look.)

∴ DJ Hannibal | 14-Dec-2005 2:31pm est | #8849

Hans (http://zephyrfalcon.org/) wrote:

With a prototype-based object system the distinction between metaclasses and classes (heck, and objects) goes away, so it seems to me that you could do the same thing as what Python calls metaclasses simply by overriding a particular object's clone method. Am I wrong?

No, IMHO, with a prototype-based system, a whole slew of problems and unnecessary features of class-based languages just disappears. Since there is no distinction between classes and instances anymore, there's no need for metaclasses, classes, or class methods.

It's more or less possible to write a prototype-based system in Python (ala Self), by the way... but it would be clumsy to use. Been there, done that. :-)

∴ Hans | 14-Dec-2005 3:23pm est | http://zephyrfalcon.org/ | #8850

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

May 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Maps of Iraq

my husband is in Scania too..he​says it's not too bad..he's been at​worse...

Cristy: May 16, 3:54pm

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.347s.

(Used 8 db queries)