KBD

Keith Devens .com

Saturday, March 20, 2010 Flag waving
Of all tyrannies a tyranny sincerely exercised for the good of its victims may be the most oppressive. It may... – C.S. Lewis
← 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)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



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

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.128s.

(Used 8 db queries)