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

metaclass that inherits a class of that metaclass?

Hoping this isn't seeming too confusing, but I need to create a
metaclass and a class using that metaclass, such that one of the bases
of the metaclass is the class created with that metaclass. I can't
figure out a way to do this, even after trying to add the class as a
base after the classes have been created.

Is there some way I can get this to work properly?

Jul 19 '05 #1
16 1588
Why in the name of all that is holy and just would you need to do such
a thing?

Jul 19 '05 #2
because they are representing a seperate typing system outside of
python, to which I am creating a bridge. The metaclass represents the
types of this other system and the class represents the most basic
object type, but since the types the metaclass represent are also
objects, this is the only way i see to represent the relationship
properly.

Jul 19 '05 #3
I don't think that makes any sense. How could you possibly create such
a circular relationship between things in any language? Besides, if I
understand metaclasses at all, only other metaclasses can be bases of a
metaclass.

Why not use python classes to represent the other system's types with a
python metaclass as the "type" type?

Jul 19 '05 #4
On 1 Jun 2005 09:41:53 -0700, infidel <sa***********@gmail.com> wrote:
Why in the name of all that is holy and just would you need to do such
a thing?


Is anyone else amused that this came from the mouth of someone named "Infidel"?

--
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org
Jul 19 '05 #5
because i need the representations of the other systems types to
themselves be python classes, and so i need a metaclass to make sure
they follow certain rules. This metaclass is for that system what type
is for python, and type is an object, which is a type. same thing, no?

Jul 19 '05 #6
"God made me an atheist, who are you to question His wisdom?"

-- Saint Infidel the Skeptic

Jul 19 '05 #7
> because i need the representations of the other systems types to
themselves be python classes, and so i need a metaclass to make sure
they follow certain rules. This metaclass is for that system what type
is for python
I think that's exactly the same thing I just said. More or less.
Although depending on exactly what you mean by "follow certain rules",
you might only need a common base class rather than a metaclass.
same thing, no?


Uh, no, I don't think so. type is, from my trivial understanding, the
base type and base metaclass for everything else in python. Saying
"type is an object" is only confusing you into thinking it is a
subclass of object, which is not the case. object is a class, which I
believe has type as it's metaclass (though I could be mistaken - this
gets terribly confusing very quickly).

Jul 19 '05 #8
ironfroggy wrote:
because they are representing a seperate typing system outside of
python, to which I am creating a bridge.


Since a type-hierarchy is a tree also a subtree of it is a
type-hierarchy. You only have to map the root of a sub-hierarchy of
Python classes to the root of the hierarchy of the other typing system
and create an isomorphism between types. For exactly the same reason
you can map Pythons type hierarchy onto a sub-hierarchy of it. This
might not be completely sufficient because there are functions that
operate on types ( like mro(), isinstance(), type() etc. ). Those must
be mapped as well to counterparts of the other type-system. In
contemporary CS slang this is called a 'Functor of Categories' with
objects that are types ( and boolean values like True, False ) and
arrows that are type aware functions like those listed above.

Kay

Jul 19 '05 #9
In article <11*********************@z14g2000cwz.googlegroups. com>,
"infidel" <sa***********@gmail.com> wrote:
[ ... ] type is, from my trivial understanding, the
base type and base metaclass for everything else in python. Saying
"type is an object" is only confusing you into thinking it is a
subclass of object, which is not the case.
Sure is:
type.__bases__ (<type 'object'>,)
object is a class, which I
believe has type as it's metaclass (though I could be mistaken - this
gets terribly confusing very quickly).


Correct:
object.__class__ <type 'type'>

type also has type as its metaclass:
type.__class__

<type 'type'>

In other words, type is an instance of itself.

Just
Jul 19 '05 #10
Oh great, just when I thought I was starting to grok this mess.

Jul 19 '05 #11
Ok, forget everything I've said. The more I think about this the less
I understand it. I'm way out of my league here.

sitting-down-and-shutting-up-ly y'rs,

infi

Jul 19 '05 #12
infidel a écrit :
Oh great, just when I thought I was starting to grok this mess.


+1

dont-worry-no-normal-humain-brain-can-handle-this-kind-of-stuff-ly'yrs

Jul 19 '05 #13
Kristian Zoerhoff a écrit :
On 1 Jun 2005 09:41:53 -0700, infidel <sa***********@gmail.com> wrote:
Why in the name of all that is holy and just would you need to do such
a thing?

Is anyone else amused that this came from the mouth of someone named "Infidel"?


I was just recovering from a ROFL attack when I noticed this too...
Guess what was the result ?

Jul 19 '05 #14
ironfroggy wrote:
Hoping this isn't seeming too confusing, but I need to create a
metaclass and a class using that metaclass, such that one of the bases
of the metaclass is the class created with that metaclass. I can't
figure out a way to do this, even after trying to add the class as a
base after the classes have been created.

Is there some way I can get this to work properly?

What do you have, and how doesn't it work?

I get:
class meta(type): pass ... class cls(object): ... __metaclass__ = meta
... def __repr__(cls):
... return "I'm %s, an instance of %s" % (cls.__name__, type(cls))
... meta.__bases__ = (cls,)+meta.__bases__
cls I'm cls, an instance of <class 'meta'> assert type(cls) in cls.__subclasses__()


Of course, re-assigning meta.__bases__ comes too late to affect the construction
sequence of cls.

Michael

Jul 19 '05 #15
infidel wrote:
Ok, forget everything I've said. The more I think about this the less
I understand it. I'm way out of my league here.

sitting-down-and-shutting-up-ly y'rs,

Well, that's a sign of maturity right there ;-)

some-people-just-don't-know-when-to-ly y'rs - steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 19 '05 #16
You should read the metaclass book, if you can find it.
For the reference, see

http://www-106.ibm.com/developerwork.../l-pymeta.html
http://www-128.ibm.com/developerwork...ta2/index.html

Michele Simionato

Jul 19 '05 #17

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...
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...
2
by: lbolognini | last post by:
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...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.