473,382 Members | 1,720 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,382 software developers and data experts.

module wide metaclass for new style classes

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( name )
return _meta

reg = [ ]
__metaclass__ = meta( reg )

class c1:
pass

class c2:
pass

print reg
This would correctly print [ 'c1', 'c2' ]. Now I would like to switch
to new style classes but replacing class c1: and class c2: by class
c1(object): and class c2(object): doesn't work because the metaclass
associated with object will be called and not mine. Of course if I
would add __metaclass__ = meta(reg) to all class definitions that
would work, but how do I do this on the module level?
Dec 16 '06 #1
3 1486
On 16 dic, 14:44, "Daniel Nogradi" <nogr...@gmail.comwrote:
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( name )
return _meta

reg = [ ]
__metaclass__ = meta( reg )

class c1:
pass

class c2:
pass

print reg

This would correctly print [ 'c1', 'c2' ]. Now I would like to switch
to new style classes but replacing class c1: and class c2: by class
c1(object): and class c2(object): doesn't work because the metaclass
associated with object will be called and not mine. Of course if I
would add __metaclass__ = meta(reg) to all class definitions that
would work, but how do I do this on the module level?
That code doesn't work even for classic classes. Anyway, you don't have
to add __metaclass__ everywhere, only to the base class(es).

If all you want to register is contained in a single module, I prefer a
function that iterates over all defined classes:

from inspect import isclass
for obj in globals().values():
if isclass(obj): # may be stricter too, like issubclass(obj, Base)
reg.append(obj)

--
Gabriel Genellina

Dec 17 '06 #2
Daniel Nogradi wrote:
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( name )
return _meta

reg = [ ]
__metaclass__ = meta( reg )

class c1:
pass

class c2:
pass

print reg
That code does not create classes. Here's a slightly simplified version:
>reg = []
>>def __metaclass__(name, bases, dict):
.... reg.append(name)
....
>>class A: pass
....
>>reg
['A']
>>A is None
True # oops!
This would correctly print [ 'c1', 'c2' ]. Now I would like to switch
to new style classes but replacing class c1: and class c2: by class
c1(object): and class c2(object): doesn't work because the metaclass
associated with object will be called and not mine. Of course if I
would add __metaclass__ = meta(reg) to all class definitions that
would work, but how do I do this on the module level?
If present, __metaclass__ serves as a factory for classes without an
explicit base class. For example,

__metaclass__ = type

would turn c1 and c2 into newstyle classes (that inherit from object).
If you want side effects you can wrap the metaclass into a function:
>>reg = []
def __metaclass__(name, bases, dict):
.... reg.append(name)
.... return type(name, bases, dict)
....
>>class A: pass
....
>>class B: pass
....
>>reg
['A', 'B']
>>issubclass(A, object)
True

Peter

Dec 17 '06 #3
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( name )
return _meta

reg = [ ]
__metaclass__ = meta( reg )

class c1:
pass

class c2:
pass

print reg

That code does not create classes. Here's a slightly simplified version:
reg = []
def __metaclass__(name, bases, dict):
... reg.append(name)
...
>class A: pass
...
>reg
['A']
>A is None
True # oops!
This would correctly print [ 'c1', 'c2' ]. Now I would like to switch
to new style classes but replacing class c1: and class c2: by class
c1(object): and class c2(object): doesn't work because the metaclass
associated with object will be called and not mine. Of course if I
would add __metaclass__ = meta(reg) to all class definitions that
would work, but how do I do this on the module level?

If present, __metaclass__ serves as a factory for classes without an
explicit base class. For example,

__metaclass__ = type

would turn c1 and c2 into newstyle classes (that inherit from object).
If you want side effects you can wrap the metaclass into a function:
>reg = []
def __metaclass__(name, bases, dict):
... reg.append(name)
... return type(name, bases, dict)
...
>class A: pass
...
>class B: pass
...
>reg
['A', 'B']
>issubclass(A, object)
True
Thanks a lot, and sorry for the copy-paste error, I had the 'return
type(...)' line but didn't know that this already creates new-style
classes.
Dec 17 '06 #4

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

Similar topics

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...
5
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
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...
1
by: RayS | last post by:
I've begun a Python module to provide a complete interface to the Meade LX200 command set, and have searched for a style/development guide for Python Lib/site-packages type modules, but only saw...
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...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
9
by: Ramashish Baranwal | last post by:
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in...
3
by: Jens Thiede | last post by:
I don't like the property function, usable in the new-style classes, because having to remember to manage a list of "foo = property(...)" assignments just plain sucks, so I wrote a metaclass that...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.