473,406 Members | 2,769 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,406 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 1949
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(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.