473,702 Members | 2,357 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 #1
27 2005
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
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.
In this case, you shouldn't worry about that, since presumably you're
just using it during development so it'll never be called anyway in the
release version.
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.
You could just put the function call in your base class's __init__
method. (Or, in fact, put the code from that call directly there, if
you only care when you inherit from the one Zope class.)
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.
__init__ method, I believe. But I just realized something - how does
Prothon (or prototyped languages in general) handle multiple
inheritance or mixins?
[*] 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.


http://www.python.org/2.2/descrintro.html#metaclasses has some
references you might find interesting.

Joe
Jul 18 '05 #2
Joe Mason <jo*@notcharles .ca> wrote in message news:<sl******* *********@gate. notcharles.ca>. ..

You could just put the function call in your base class's __init__
method. (Or, in fact, put the code from that call directly there, if
you only care when you inherit from the one Zope class.)


Uh? The base class __init__ method is not called when I derive a new
class. I want to make the make the check at class level, *before*
instantiating.

Michele Simionato
Jul 18 '05 #3
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
Joe Mason <jo*@notcharles .ca> wrote in message news:<sl******* *********@gate. notcharles.ca>. ..

You could just put the function call in your base class's __init__
method. (Or, in fact, put the code from that call directly there, if
you only care when you inherit from the one Zope class.)


Uh? The base class __init__ method is not called when I derive a new
class. I want to make the make the check at class level, *before*
instantiating.


It is if you call the inherited __init__ method, which you should be
doing because it probably does important stuff.

Why do you care whether it checks before instantiating the class, or
instantiates the class and then throws an error during __init__? It's
not going to make any speed or memory difference to the successful case,
which is all you care about for performance.

Joe
Jul 18 '05 #4

"Joe Mason" <jo*@notcharles .ca> wrote in message
news:sl******** ********@gate.n otcharles.ca...
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
Joe Mason <jo*@notcharles .ca> wrote in message news:<sl******* *********@gate. notcharles.ca>. ..

You could just put the function call in your base class's __init__
method. (Or, in fact, put the code from that call directly there, if
you only care when you inherit from the one Zope class.)


Uh? The base class __init__ method is not called when I derive a new
class. I want to make the make the check at class level, *before*
instantiating.


It is if you call the inherited __init__ method, which you should be
doing because it probably does important stuff.


?? The derived class __init__ is not called until the first instance is
created, which might be way later or even never in any particular run of
the program. It does *not* check '*before* instantiating' as M.S. sensibly
wants.
Why do you care whether it checks before instantiating the class, or
instantiates the class and then throws an error during __init__? It's
not going to make any speed or memory difference to the successful case,
which is all you care about for performance.


The derived class is created just once. __init__ is called for every
instance, which could be a large number.

Terry J. Reedy


Jul 18 '05 #5
> how does Prothon (or prototyped languages in general) handle multiple
inheritance or mixins?

There is no difference. Prothon just has multiple protoypes with all the
normal problems (like the diamond problem) and the normal solutions.

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.

"Joe Mason" <jo*@notcharles .ca> wrote in message
news:sl******** ********@gate.n otcharles.ca...
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
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.
In this case, you shouldn't worry about that, since presumably you're
just using it during development so it'll never be called anyway in the
release version.
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.


You could just put the function call in your base class's __init__
method. (Or, in fact, put the code from that call directly there, if
you only care when you inherit from the one Zope class.)
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.


__init__ method, I believe. But I just realized something - how does
Prothon (or prototyped languages in general) handle multiple
inheritance or mixins?
[*] 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.


http://www.python.org/2.2/descrintro.html#metaclasses has some
references you might find interesting.

Joe

Jul 18 '05 #6
> BTW, I wonder how Prothon would solve this problem

Me too. I'll have to take a stab at it.

Mark Hahn (Prothon author daring to raise his head in the Python forum).

"Michele Simionato" <mi************ ***@poste.it> wrote in message
news:95******** *************** ***@posting.goo gle.com...
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 #7
In article <ma************ *************** **********@pyth on.org>, Terry Reedy wrote:
?? The derived class __init__ is not called until the first instance is
created, which might be way later or even never in any particular run of
the program. It does *not* check '*before* instantiating' as M.S. sensibly
wants.


You're right, I was misreading the whole time.

Joe
Jul 18 '05 #8
Joe Mason wrote:
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
Uh? The base class __init__ method is not called when I derive a new
class. I want to make the make the check at class level, *before*
instantiating .


It is if you call the inherited __init__ method, which you should be
doing because it probably does important stuff.


But presumably he wants the check done only once, when a subclass
is defined, *not* every time said subclass is instantiated. In
Prothon it appears that both of these end up invoking the same
__init__ method, so there's no way of telling them apart.

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

Jul 18 '05 #9
> 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). You need to unthink
classes when designing in Prothon. If you try to move a problem from Python
to Prothon and think in the same classes/subclasses metaphors you won't get
very far.

Mark Hahn (Prothon Author)

"Greg Ewing (using news.cis.dfn.de )" <ie*******@snea kemail.com> wrote in
message news:c4******** *****@ID-169208.news.uni-berlin.de...
Joe Mason wrote:
In article <95************ **************@ posting.google. com>, Michele Simionato wrote:
Uh? The base class __init__ method is not called when I derive a new
class. I want to make the make the check at class level, *before*
instantiating .


It is if you call the inherited __init__ method, which you should be
doing because it probably does important stuff.


But presumably he wants the check done only once, when a subclass
is defined, *not* every time said subclass is instantiated. In
Prothon it appears that both of these end up invoking the same
__init__ method, so there's no way of telling them apart.

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

Jul 18 '05 #10

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

Similar topics

0
1396
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
1309
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
6302
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
3558
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
1831
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
2609
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
1803
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
8652
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
9234
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
9086
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...
1
8979
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3104
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
2
2399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.