473,289 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Metaclass vs Class factory

Hi all,

I dare risk my brain exploding by reaching for the understanding of
metaclasses.

At first i thought i almost got them, even if vaguely back in a corner
of my mind, my understanding was that, as classes' class a metaclass
would be able to return a different class based on input...

.... until i thought of factory functions and, Python considering
classes just another first-class object, as such i started considering
them.

So is anybody experienced in the dark side semantics of Python willing
to explain the difference, in simple terms, between a metaclass and a
function that returns a class?

Thanks,
Lorenzo

Oct 30 '07 #1
2 1945
lb********@gmail.com a écrit :
Hi all,

I dare risk my brain exploding by reaching for the understanding of
metaclasses.

At first i thought i almost got them, even if vaguely back in a corner
of my mind, my understanding was that, as classes' class a metaclass
would be able to return a different class based on input...

... until i thought of factory functions and, Python considering
classes just another first-class object, as such i started considering
them.

So is anybody experienced in the dark side semantics of Python willing
to explain the difference, in simple terms, between a metaclass and a
function that returns a class?
The same as the difference between a class and a function that returns
an instance.

One of the main use of metaclasses it to allow "postprocessing" of the
class object - ie, to automatically add extra features to a class,
usually based on the class definition. This is mostly useful for
framework stuff, where it can avoids quite a lot of boilerplate. IOW,
the 'input' of a metaclass is often the class object itself. The
metaclass constructor (I mean the proper constructor, __new__) let you
play with the class bases and attributes before the class object is
instanciated, and the initializer (__init__) let you modify the class
object after instanciation.

You can of course use the metaclass constructor as a class factory
(which it is), but it's obviously overkill if all you need is to
dynamically 'select' a class based on either inputs and/or environment
(configuration, platform, whatnot).

HTH
Oct 30 '07 #2
On Oct 30, 10:46 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
The same as the difference between a class and a function that returns
an instance.
Thanks Bruno.

Lorenzo

Oct 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
2
by: zipher | last post by:
After searching through comp.lang.python and the web regarding metaclasses, I could not find an example for customing classes using metaclass parameters. I want to be able to create a class at...
39
by: Nicolas Fleury | last post by:
In the following example: class MyMetaclass(type): pass class MyBaseType(object): __metaclass__ = MyMetaclass class MyType(MyBaseType): x = 4 y = 5 z = 6 Is there any way to modify...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
3
by: Daniel Nogradi | last post by:
I used to have the following code to collect all (old style) class names defined in the current module to a list called reg: def meta( reg ): def _meta( name, bases, dictionary ): reg.append(...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.