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

circular dependency between class and its metaclass

hi there,

I'v run into a little problem for which I only found an ugly workaround,
so I'd like to know whether people are aware of better ways to achieve this...

I'm defining a class 'Class' for which I want to set up a __metaclass__ 'Type'
which does some work on class variables. In particular, it should apply
a function on all base classes that are themself derived from 'Class'.

My first try was:

====

class Class(object):

class Type(type):

def __init__(cls, name, bases, dict):

hierarchy = list(filter(lambda i:issubclass(i, Class), bases))
# do something with the hierarchy here

__metaclass__ = Type

====

However, this fails because at the point where I want to create the
'hierarchy' variable 'Class' isn't known yet. (At what point is it actually
injected into the local dict ?)

Then I tried to define 'Type' after 'Class' (instead of inside it):

====

class Class(object): pass #supress details for clarity

class Type(type):
...

Class.__metaclass__ = Type

====

But this didn't work at all (even though I don't quite understand why).

Then I tried a workaround using an auxiliary base class:

====

class Base(object): pass #supress details for clarity

class Type(type):
def __init__(cls, name, bases, dict):

# now base the filter on 'Base'
hierarchy = list(filter(lambda i:issubclass(i, Base), bases))
# do something with the hierarchy here

class Class(Base):

__metaclass__ = Type

...

====

though this is quite ugly, as 'Class' is the class that provides the stuff
the 'Type' constructor should act on, not 'Base' (so to make the filter above
work I have to do some more ugly tricks).

I'd very much appreciate if anybody could shed some light on why python
behaves the way it does in the two first cases, and whether there are
better ways to achieve what I want. Thanks a lot !

Stefan
Jul 18 '05 #1
1 1942
Stefan Seefeld <se*****@sympatico.ca> wrote in message news:<ma**************************************@pyt hon.org>...
hi there,

I'v run into a little problem for which I only found an ugly workaround,
so I'd like to know whether people are aware of better ways to achieve this...

I'm defining a class 'Class' for which I want to set up a __metaclass__ 'Type'
which does some work on class variables. In particular, it should apply
a function on all base classes that are themself derived from 'Class'.

My first try was:

====

class Class(object):

class Type(type):

def __init__(cls, name, bases, dict):

hierarchy = list(filter(lambda i:issubclass(i, Class), bases))
# do something with the hierarchy here

__metaclass__ = Type


If "i" is a subclass of Class, then it is an instance of Type: this means
that you can write

class Type(type):
def __init__(cls, name, bases, dict):
hierarchy = [i for i in bases if isinstance(i, Type)]
# I like list comprehension...

class Class(object):
__metaclass__ = Type

Is this nice enough for you?

Michele Simionato
Jul 18 '05 #2

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

Similar topics

1
by: Henry Miller | last post by:
I have the following code (much simplified for this post). Note that SessionKey uses DataAccess, and DataAccess requires SessionKey in it's constructor. Public Class SessionKey Public...
3
by: crichmon | last post by:
Any general advice for dealing with circular dependencies? For example, I have a situation which, when simplified, is similar to: ///////////// // A.h class A { public: int x;
2
by: ernesto basc?n pantoja | last post by:
Hi everybody: I'm implementing a general C++ framework and I have a basic question about circular dependencies: I am creating a base class Object, my Object class has a method defined as:...
4
by: ro86 | last post by:
Hello everyone! I am a newbie to C++ (~1 Week experience) and I have a few months of experience with object-oriented languages (Objective-C). I am currently working just for fun on a particle...
4
by: Henke | last post by:
I have this scenario. public class A { public int numbers; public class A() { }
2
by: Rimma Meital via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Rimma Meital I have 2 DLLs (Class Libraries). Both defines single-ton objects - logger object (writes to logger) and...
3
by: Rob Clark | last post by:
Hi, I have an issue which is the ordinary and always recurring circular dependency - but I do not know how to resolve it :-( The usual forward declaration does not help... I have a client...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
5
by: fomas87 | last post by:
Hi guys, I'm writing a framework for an sdl gui in c++, but got stuck on the circular dependency, I've read a lot of articles about resolving dependencies and about forward declaration but none of...
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...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.