473,847 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prothon, metaclasses, Zope [Was: A 'Python like' language]

> Hello, my name is Skip and I am metaclass-unaware. I've been programming in
Python for about ten years and I have yet to write a metaclass. At first I
thought it was just that metaclasses were new to the language, but now as
more and more people use them and proclaim their widespread benefits, I have
come to realize that through years of abuse my brain has become addicted to
classic classes.


I began using Python since version 2.2.1 and without knowing anything
about OOP, so I had the advantage of a fresh start ;) Still, I will readily
admit that I was not immediately sold to metaclasses and actually I was
kind of skeptical about them. The "Putting metaclasses to work" book
made me change my mind. At this point I have becomed so accustomed to
metaclasses that I am disturbed when I cannot use them.

Just a real life example. I started studying Zope few days ago.
Writing my first class I got caught since I was overriding a predefined Zope
method. I made a dir() and discovered that the context object in Zope
has more than four hundreds (400!) attributes. In such a situation it
is likely to override a predefined name, especially now that I am a
beginner and I have a fair chance of reimplementing (badly) something
which is already available. So, I thought: "well, this a job for a metaclass"
and in five minutes I implemented a metaclass raising an error if I was
inadvertently overriding a predefined name (except names such as __init__
and similia, of course). Everything was nice and good until the moment
I tested the metaclass on a Zope class and got a segmentation fault.

Since I don't know anything about Zope internals I can only make a guess
of what happened and I would be happy if some Zope guru here could
confirm (possibly educated) guess.

In my understanding, Zope tweaked Python classes at C level code, introducing
the concept of Extension classes. Extension classes are instances of a C coded
metaclass which does a lot of magic (for instance there are methods which
are automatically generated each time I create an extensions class);
unfortunately, this metaclass does not follow the protocol of Python 2.2+
metaclasses. This is evidently an historical accident, since Zope Extension
Classes were invented before Python 2.2[*]; the net result is that I cannot
mix Python classes with custom metaclasses and Zope Extension classes.

I have heard that this is a temporary wart and that Zope 3 will solve
this issue (I'd like to have a confirmation here). However, for the
moment, I had to come out with a non-metaclass solution.

So, I reverted my mind to the pre-metaclass functioning mode (which required
a certain effort) and I wrote a function that takes a class, looks at
its dictionary, and raises an error if the class overrides an attribute
which is already defined in the parent classes. The solution works,
but it is kind of ugly compared to the metaclass solution:

1. The metaclass can raise the error *before* the metaclass is created,
whereas the function works a posteriori, *after* the overriding is done;
if find it to be conceptually unsatisfactory, I don't want to create
a class just to throw it away. The alternative is to use a class factory,
but then I have just re-invented a metaclass with an ugly call syntax.

2. The metaclass can be inherited, so the check is automatic for all children;
on the contrary, I have to call the function by hand each time I define
a new class. This means writing twice the class name, which is error
prone if I later rename the class and I forget to update the function
call.

Whereas it is true that in most cases you can find a non-metaclass solution,
it is also true that in most case the metaclass solution is by far more
elegant than the alternative.

BTW, I wonder how Prothon would solve this problem, i.e. selectively
forbidding the overriding of names, with an easy of use/elegance
comparable to the Python metaclass solution.

Michele Simionato

[*] I would be curious to know if Guido decided to expose metaclasses in
Python since he noticed that they were already being used in real applications
such as Zope (in some hidden form), or if there was some other reason.
Jul 18 '05
27 2029
In article <95************ **************@ posting.google. com>,
So, I reverted my mind to the pre-metaclass functioning mode (which required
a certain effort) and I wrote a function that takes a class, looks at
its dictionary, and raises an error if the class overrides an attribute
which is already defined in the parent classes. The solution works,
but it is kind of ugly compared to the metaclass solution:

1. The metaclass can raise the error *before* the metaclass is created,
whereas the function works a posteriori, *after* the overriding is done;
if find it to be conceptually unsatisfactory, I don't want to create
a class just to throw it away. The alternative is to use a class factory,
but then I have just re-invented a metaclass with an ugly call syntax.
I would add the check to a test case for the class.

class XYZTestCase(Zop eTestCase):
tested_class = XYZ

def testOverriddenM ethods(self):
check_if_we_are _overriding_nam es_in(self.test ed_class)
2. The metaclass can be inherited, so the check is automatic for all children;
on the contrary, I have to call the function by hand each time I define
a new class. This means writing twice the class name, which is error
prone if I later rename the class and I forget to update the function
call.


Test cases can be inherited.

class XYZ2TestCase(XY ZTestCase):
tested_class = XYZ2

--
Roberto Lupi
Jul 18 '05 #11
Mark Hahn wrote:
But presumably he wants the check done only once, when a subclass is
defined, *not* every time said subclass is instantiated.


You can have a different __init__ for an object and it's prototpe.So the
Prothon equivalent of a subclass can easily have different __init__
behaviour than it's child (what you call instance).


Can you post some code illustrating how you would do this
in Prothon? I still can't see how, at the point where you
do

X = Base()

anything can tell whether you're intending to use X as
an instance of Base or whether you're going to go on to
say

with X:
def ...

and use X as a prototype which inherits behaviour from
Base. In both cases, Base.__init__ is going to get invoked
before you have a chance to define any other __init__ that
might override it.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #12
Greg: Just a quick note to let you know that I do intend to reply to this
but haven't had the time.

Greg Ewing (using news.cis.dfn.de ) wrote:
Mark Hahn wrote:
But presumably he wants the check done only once, when a subclass is
defined, *not* every time said subclass is instantiated.


You can have a different __init__ for an object and it's prototpe.So
the Prothon equivalent of a subclass can easily have different
__init__ behaviour than it's child (what you call instance).


Can you post some code illustrating how you would do this
in Prothon? I still can't see how, at the point where you
do

X = Base()

anything can tell whether you're intending to use X as
an instance of Base or whether you're going to go on to
say

with X:
def ...

and use X as a prototype which inherits behaviour from
Base. In both cases, Base.__init__ is going to get invoked
before you have a chance to define any other __init__ that
might override it.

Jul 18 '05 #13
In article <eAF9c.48978$cx 5.17895@fed1rea d04>,
Mark Hahn <ma**@prothon.o rg> wrote:

Self defines having the same attribute in two different prototypes
illegal. That seemed extremely constraining to me so I went with the
Python 2.2 mro solution in Prothon.


Bad idea. But I won't tell you why until you stop top-posting and
over-quoting.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #14
Does anyone get anything done around here with all the bitching? I've been
communicating via email for 30 years and never have I seen such complaining
about something so silly. If you have anything to contribute to Prothon
I'll be over in the Prothon lists. We are too busy creating to have
conversations like this over there.

You may not be able to tell it, but I'm a nice guy who would really like
feedback from intelligent knowledgable people like yourself. I just can't
take this anymore.

Aahz wrote:
In article <eAF9c.48978$cx 5.17895@fed1rea d04>,
Mark Hahn <ma**@prothon.o rg> wrote:

Self defines having the same attribute in two different prototypes
illegal. That seemed extremely constraining to me so I went with the
Python 2.2 mro solution in Prothon.


Bad idea. But I won't tell you why until you stop top-posting and
over-quoting.

Jul 18 '05 #15

"Greg Ewing (using news.cis.dfn.de )" <ie*******@snea kemail.com> wrote in
message news:c4******** *****@ID-169208.news.uni-berlin.de...
Mark Hahn wrote:
But presumably he wants the check done only once, when a subclass is
defined, *not* every time said subclass is instantiated.
You can have a different __init__ for an object and it's prototpe.So the
Prothon equivalent of a subclass can easily have different __init__
behaviour than it's child (what you call instance).


Can you post some code illustrating how you would do this
in Prothon? I still can't see how, at the point where you
do

X = Base()

anything can tell whether you're intending to use X as
an instance of Base or whether you're going to go on to
say

with X:
def ...

and use X as a prototype which inherits behaviour from
Base. In both cases, Base.__init__ is going to get invoked
before you have a chance to define any other __init__ that
might override it.


You don't have to use the obj = proto() technique to create an object. The
following code is identical to saying subproto = topproto() except no
__init__() will be called:

subproto = Object()
subproto.set_pr oto(topproto) # switch prototype on the fly

Does this give you the missing piece you need or is there something else
missing in Prothon? If you explain the overall goal better I'll write the
complete code for you.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #16
"Mark Hahn" <ma**@prothon.o rg> wrote in message news:<S7Lac.876 28$cx5.10253@fe d1read04>...

Original top posting maintained on purpose:
Does anyone get anything done around here with all the bitching? I've been
communicating via email for 30 years and never have I seen such complaining
about something so silly. If you have anything to contribute to Prothon
I'll be over in the Prothon lists. We are too busy creating to have
conversations like this over there.

You may not be able to tell it, but I'm a nice guy who would really like
feedback from intelligent knowledgable people like yourself. I just can't
take this anymore.

Aahz wrote:
In article <eAF9c.48978$cx 5.17895@fed1rea d04>,
Mark Hahn <ma**@prothon.o rg> wrote:

Self defines having the same attribute in two different prototypes
illegal. That seemed extremely constraining to me so I went with the
Python 2.2 mro solution in Prothon.


Bad idea. But I won't tell you why until you stop top-posting and
over-quoting.


Aahz is probably referring to the fact that Python 2.3 has changed the
MRO since the 2.2 one was a bad idea. See
http://www.python.org/2.3/mro.html
for more. A part for this, Aahz is right in pointing out that you
cannot get
the attention of a lot of intelligent knowledgable people on c.l.py
because
of your postings habits. Actually this is the reason why I have
downloaded Prothon yet. It is kinda of a pity, since maybe the idea is
worth, but it is difficult to give credit to people liking tabs and
top posting. Those are not
stupid things IMNSHO.

Michele Simionato
Jul 18 '05 #17
In article <95************ **************@ posting.google. com>,
Michele Simionato <mi************ ***@poste.it> wrote:

[...]


Michele, I just got a mailbox full error when I tried sending you
e-mail. Please fix.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #18
> it is difficult to give credit to people liking tabs and
top posting.
Even though I admit to the heinous crime being a "tab-lover", we have
changed Prothon to accept either tabs or spaces in indents, just not both in
the same file.

As far as top-posting, I am trying to improve, it's just hard to change my
reply style after using it for 30 years without getting any complaints
before. I feel like I'm in a twilight-zone episode.

"Michele Simionato" <mi************ ***@poste.it> wrote in message
news:95******** *************** ***@posting.goo gle.com... "Mark Hahn" <ma**@prothon.o rg> wrote in message news:<S7Lac.876 28$cx5.10253@fe d1read04>...
Original top posting maintained on purpose:
Does anyone get anything done around here with all the bitching? I've been communicating via email for 30 years and never have I seen such complaining about something so silly. If you have anything to contribute to Prothon
I'll be over in the Prothon lists. We are too busy creating to have
conversations like this over there.

You may not be able to tell it, but I'm a nice guy who would really like
feedback from intelligent knowledgable people like yourself. I just can't take this anymore.

Aahz wrote:
In article <eAF9c.48978$cx 5.17895@fed1rea d04>,
Mark Hahn <ma**@prothon.o rg> wrote:
>
> Self defines having the same attribute in two different prototypes
> illegal. That seemed extremely constraining to me so I went with the
> Python 2.2 mro solution in Prothon.

Bad idea. But I won't tell you why until you stop top-posting and
over-quoting.


Aahz is probably referring to the fact that Python 2.3 has changed the
MRO since the 2.2 one was a bad idea. See
http://www.python.org/2.3/mro.html
for more. A part for this, Aahz is right in pointing out that you
cannot get
the attention of a lot of intelligent knowledgable people on c.l.py
because
of your postings habits. Actually this is the reason why I have
downloaded Prothon yet. It is kinda of a pity, since maybe the idea is
worth, but it is difficult to give credit to people liking tabs and
top posting. Those are not
stupid things IMNSHO.

Michele Simionato

Jul 18 '05 #19
"Mark Hahn" <ma**@prothon.o rg> schreef:
As far as top-posting, I am trying to improve, it's just hard to
change my reply style after using it for 30 years without getting any
complaints before. I feel like I'm in a twilight-zone episode.


You're still not using OE-QuoteFix... ;-)

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #20

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

Similar topics

0
1404
by: Mark Hahn | last post by:
I would like to announce a new interpreted object-oriented language very closely based on Python, that is Prototype-based, like Self (http://research.sun.com/research/self/language.html) instead of class-based like Python. I have named the language Prothon, short for PROtotype pyTHON. You can check it out at http://prothon.org. The prototype scheme makes object oriented computing very simple and complicated things like meta-classes...
0
1319
by: Mark Hahn | last post by:
Ben Collins and I have developed a new interpreted object-oriented language very closely based on Python, that is Prototype-based, like Self (http://research.sun.com/research/self/language.html) instead of class-based like Python. I have named the language Prothon, short for PROtotype pyTHON. You can check it out at http://prothon.org. The prototype scheme makes object oriented computing very simple and complicated things like...
145
6427
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity unecessary? Here is an example of a Python class with all three types of methods (instance, static, and class methods). # Example from Ch.23, p.381-2 of Learning Python, 2nd ed. class Multi:
7
3567
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful enough to implementing prototypes in pure Python. I wrote a module that implements part of what...
25
1851
by: Mark Hahn | last post by:
There is a new release of Prothon that I think is worth mentioning here. Prothon version 0.1.0 has changed almost beyond recognition compared to what was discussed here before. For example: the "Perl-like" symbols are gone and the "self" keyword is back, replacing the period. Prothon has gotten more "python-like", simpler, and more powerful, all at the same time. There is a new tutorial that covers Prothon completely without assuming...
49
2639
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon code sample where newbies expect the two function calls below to both print : def f( list= ): print list.append!(1) f() # prints
20
1824
by: Mark Hahn | last post by:
Prothon is pleased to announce another major release of the language, version 0.1.2, build 710 at http://prothon.org. This release adds many new features and demonstrates the level of maturity that Prothon has reached. The next release after this one in approximately a month will be the first release to incorporate the final set of frozen Prothon 1.0 language features and will be the Alpha release. You can see the set of features still...
0
9886
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10983
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10647
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9482
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7882
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.