By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

metaclass that inherits a class of that metaclass?

P: n/a
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
Share this Question
Share on Google+
16 Replies


P: n/a
Why in the name of all that is holy and just would you need to do such
a thing?

Jul 19 '05 #2

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"God made me an atheist, who are you to question His wisdom?"

-- Saint Infidel the Skeptic

Jul 19 '05 #7

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
Oh great, just when I thought I was starting to grok this mess.

Jul 19 '05 #11

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.