473,753 Members | 7,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prothon Prototypes vs Python Classes

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:
numInstances = 0
def __init__(self):
Multi.numInstan ces += 1
def printNumInstanc es():
print "Number of Instances:", Multi.numInstan ces
printNumInstanc es = staticmethod(pr intNumInstances )
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cme th)

a = Multi(); b = Multi(); c = Multi()

Multi.printNumI nstances()
a.printNumInsta nces()

Multi.cmeth(5)
b.cmeth(6)
Here is the translation to Prothon.

Multi = Object()
with Multi:
.numInstances = 0
def .__init__(): # instance method
Multi.numInstan ces += 1
def .printNumInstan ces(): # static method
print "Number of Instances:", Multi.numInstan ces
def .cmeth(x): # class method
print Multi, x

a = Multi(); b = Multi(); c = Multi()

Multi.printNumI nstances()
a.printNumInsta nces()

Multi.cmeth(5)
b.cmeth(6)
Note the elimination of 'self' in these methods. This is not just a
syntactic shortcut (substiting '.' for 'self') By eliminating this
explicit passing of the self object, Prothon makes all method forms
the same and eliminates a lot of complexity. It's beginning to look
like the complexity of Python classes is unecessary.

My question for the Python experts is -- What user benefit are we
missing if we eliminate classes?

-- Dave

Jul 18 '05 #1
145 6347

"David MacQuigg" <dm*@gain.com > wrote in message
news:56******** *************** *********@4ax.c om...
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:
numInstances = 0
def __init__(self):
Multi.numInstan ces += 1
def printNumInstanc es():
print "Number of Instances:", Multi.numInstan ces
printNumInstanc es = staticmethod(pr intNumInstances )
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cme th)

a = Multi(); b = Multi(); c = Multi()

Multi.printNumI nstances()
a.printNumInsta nces()

Multi.cmeth(5)
b.cmeth(6)
Here is the translation to Prothon.

Multi = Object()
with Multi:
.numInstances = 0
def .__init__(): # instance method
Multi.numInstan ces += 1
def .printNumInstan ces(): # static method
print "Number of Instances:", Multi.numInstan ces
def .cmeth(x): # class method
print Multi, x

a = Multi(); b = Multi(); c = Multi()

Multi.printNumI nstances()
a.printNumInsta nces()

Multi.cmeth(5)
b.cmeth(6)
Note the elimination of 'self' in these methods. This is not just a
syntactic shortcut (substiting '.' for 'self') By eliminating this
explicit passing of the self object, Prothon makes all method forms
the same and eliminates a lot of complexity. It's beginning to look
like the complexity of Python classes is unecessary.
You're mixing two different issues here. The first one I'm going
to address is namespaces. I tend to agree with you, and I've
said it before; most languages would benefit a lot by putting
different kinds of identifiers in lexically distinct namespaces, and
letting the editors take up the slack. We expect modern editors to
colorize keywords, why not let them do some of the dogwork?
My question for the Python experts is -- What user benefit are we
missing if we eliminate classes?
Duh? Again, as far as I can tell, classes are a leftover idea from
static typing. However, you should think of a lot of the standard
OO patterns. We have, for example, classes which are never
intended to be instantiated, and classes that are, and classes that
have only one instance (singletons.) In interactive fiction (and I
suspect other application areas) having a class with more than
one instance is rare (but not unheard of.)

In more normal applications, you usually have a large number of
instances of any given class; those instances don't vary in their
behavior so it makes sense to optimize access by distinguishing
between the class and the instance. This is, in fact, what Python
does. It has syntactic support for creating instances of type "class",
complete with builtin support for methods and fancy initialization
options, and a very abbreviated syntax for the much more prevalent
operation of creating instances of type "instance."

I've seen another recent comment to the effect that eliminating
classes would remove a very valuable level of abstraction for
larger programs. The idea is superficially attractive. I'd like to see
examples of very large systems done in a prototype based language
and how they handled the problem, or if it was, indeed, a problem.

I doubt very much if I'm going to find that one extreme or the
other is "correct." It seems to differ by what you want to do
with it.

John Roth
-- Dave

Jul 18 '05 #2
I'm not terribly familiar with the concept of prototypes although I
believe I understand the basic meaning and I have worked in languages
which do use prototypes (although not called that).

Aren't there many times when it is usefult to work with classes where
you do not want an instance to exist? Such as multiple level of
subclasses where code is kept easily readable by putting it on a the
appropiate class for that function alone. Often useful when you'll be
using many similar but not identical objects? I can see how it'd be
useful to be able to define new methods on an object, or edit the
methods on an object but I can not see the purpose to removing classes
altogether. Am I correct in my impression that with prototyping you must
instantate an object to define it's structure? That would seem wasteful
and cluttering of the namespace. Also he way your prototypes read appear
less clear to me than a class definition. I fear that you'd wind up with
people creating an object.. coding random stuff.. adding a method to
that object.. coding more random stuff.. and then adding more to the
object.
Jul 18 '05 #3

"Michael" <mo*****@mlug.m issouri.edu> wrote in message
news:40******** ******@mlug.mis souri.edu...
I'm not terribly familiar with the concept of prototypes although I
believe I understand the basic meaning and I have worked in languages
which do use prototypes (although not called that).

Aren't there many times when it is usefult to work with classes where
you do not want an instance to exist? Such as multiple level of
subclasses where code is kept easily readable by putting it on a the
appropiate class for that function alone. Often useful when you'll be
using many similar but not identical objects? I can see how it'd be
useful to be able to define new methods on an object, or edit the
methods on an object but I can not see the purpose to removing classes
altogether. Am I correct in my impression that with prototyping you must
instantate an object to define it's structure? That would seem wasteful
and cluttering of the namespace. Also he way your prototypes read appear
less clear to me than a class definition. I fear that you'd wind up with
people creating an object.. coding random stuff.. adding a method to
that object.. coding more random stuff.. and then adding more to the
object.


It's certainly true that in a prototype based language all objects
exist: there are no objects that the compiler deals with but does
not put into the resulting program. And it's quite true that it does
open up the floodgates for a lot of messiness.

On the other hand, there are application areas where that is
exactly what you need: I've already mentioned interactive fiction,
and there are undoubtedly others.

I'd like to play around with a prototype based language to see how it
works, although for reasons I've mentioned elsewhere, I won't use
Prothon. I'd be using IO if they had a windows executable installer,
rather than requiring me to compile the silly thing.

John Roth
Jul 18 '05 #4
> although for reasons I've mentioned elsewhere, I won't use Prothon.

Can you please point me to those reasons?

"John Roth" <ne********@jhr othjr.com> wrote in message
news:10******** *****@news.supe rnews.com...

"Michael" <mo*****@mlug.m issouri.edu> wrote in message
news:40******** ******@mlug.mis souri.edu...
I'm not terribly familiar with the concept of prototypes although I
believe I understand the basic meaning and I have worked in languages
which do use prototypes (although not called that).

Aren't there many times when it is usefult to work with classes where
you do not want an instance to exist? Such as multiple level of
subclasses where code is kept easily readable by putting it on a the
appropiate class for that function alone. Often useful when you'll be
using many similar but not identical objects? I can see how it'd be
useful to be able to define new methods on an object, or edit the
methods on an object but I can not see the purpose to removing classes
altogether. Am I correct in my impression that with prototyping you must
instantate an object to define it's structure? That would seem wasteful
and cluttering of the namespace. Also he way your prototypes read appear
less clear to me than a class definition. I fear that you'd wind up with
people creating an object.. coding random stuff.. adding a method to
that object.. coding more random stuff.. and then adding more to the
object.


It's certainly true that in a prototype based language all objects
exist: there are no objects that the compiler deals with but does
not put into the resulting program. And it's quite true that it does
open up the floodgates for a lot of messiness.

On the other hand, there are application areas where that is
exactly what you need: I've already mentioned interactive fiction,
and there are undoubtedly others.

I'd like to play around with a prototype based language to see how it
works, although for reasons I've mentioned elsewhere, I won't use
Prothon. I'd be using IO if they had a windows executable installer,
rather than requiring me to compile the silly thing.

John Roth

Jul 18 '05 #5
I'm not terribly familiar with the concept of prototypes although I
believe I understand the basic meaning and I have worked in languages
which do use prototypes (although not called that).

Aren't there many times when it is usefult to work with classes where
you do not want an instance to exist? Such as multiple level of
subclasses where code is kept easily readable by putting it on a the
appropiate class for that function alone. Often useful when you'll be
using many similar but not identical objects? I can see how it'd be
useful to be able to define new methods on an object, or edit the
methods on an object but I can not see the purpose to removing classes
altogether. Am I correct in my impression that with prototyping you must
instantate an object to define it's structure? That would seem wasteful
and cluttering of the namespace. Also he way your prototypes read appear
less clear to me than a class definition. I fear that you'd wind up with
people creating an object.. coding random stuff.. adding a method to
that object.. coding more random stuff.. and then adding more to the
object.

Jul 18 '05 #6
In article <56************ *************** *****@4ax.com>, David MacQuigg wrote:
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?


Complexity's ok if it's in the right place - down deep in the heart of
things where you only have to get it right once. If you simplify the
language, it can push the complexity out to the API level where
everybody has to deal with it. (However, see
http://www.ai.mit.edu/docs/articles/...tion3.2.1.html for a
counterargument .)

Ed Suominen just posted a situation where classes and instances need to
be treated separately. He has a class, "AutoText", with various
subclasses (which I'll call "AutoA", "AutoB", etc. because I forget the
details). He wants to notice whenever somebody creates a subclass of
AutoText and add it to a list so that he can have an AutoAll function
which creates just one instance of each. (Or something like that.)

# all examples below will use the same AutoAll()
autoall = []
def AutoAll():
"Return an instance of each 'class'"
l = []
for i in autoall:
l.append(i())
return l

In Python, the easiest way to do this is with metaclasses, which is a
really advanced topic. In Prothon, it's simple to "notice" when a
subclass is created, because __init__ gets called! So you just say:

# This is library code - complexity goes here
AutoText = Base()
with AutoText:
def .__init__():
Base.__init__()
autoall.append( self)

# Users of the library create new subclasses
AutoA = AutoText()

AutoB = AutoText()
with AutoB:
def .__init__(somep arams):
# do stuff with someparams
AutoText.__init __()

The problem with this is that it doesn't work. In AutoAll(), when you
call i() and create a new instance of AutoText, it adds it to autoall,
so now you have an infinite loop as autoall keeps growing.

So you need to build in some way to distinguish classes to add to
autoall from ones you don't. Say, a register() method:

# library code
AutoText = Base()
with AutoText:
# __init__ no longer needed
def .register():
autoall.append( self)

# user code
AutoA = AutoText()
with AutoA: .register()

AutoB = AutoText()
with AutoB:
def .__init__(somep arams):
# do stuff with someparams
AutoText.__init __()
.register()

That's not all that bad, really - if you ignore metaclasses, the only
way I can think of to do this in standard Python is the same, since you
only have a hook when an object is created, not a class:

# library code
class AutoText(Base):
def register(klass) :
autoall.append( klass)
register = classmethod(reg ister)
AutoText.regist er()

# user code
class AutoA(AutoText) : pass
AutoA.register( )

class AutoB(AutoText) :
def __init__(self, someparams):
# do stuff with someparams
AutoText.__init __(self)
AutoB.register( )

But with metaclasses, you can set a metaclass on AutoText which
automatically registers each subclass as it's created, so that the
explicit register() method can be skipped.

# library code - the most complex yet
class AutoRegister(ty pe):
def __init__(klass, name, bases, dict):
autoall.append( klass)

class AutoText(Base):
__metaclass__ = AutoRegister
def __init__(self, text):
Base.__init__(s elf, tet)

# user code - the simplist yet
class AutoA(AutoText) : pass

class AutoB(AutoText) :
def __init__(self, someparams):
AutoText.__init __(self)

So here's one point where the simplification of prototypes actually ends
up making the user code more complicated than the full Python version.
As a user of that code, you have no reason at all to care about
__metaclass__ and what it does - you just need to know that AutoAll()
magically creates an instance of AutoText and all it's subclasses.

If you ever need to actually look at the definition of AutoText, though,
you'll see this __metaclass__ thing which is a little harder to
understand. In the simpler version, the basics of the library are
easier, which means if you have to debug it you'll have an easier time,
but you have to keep calling register(). Which complexity is better is
a matter of philosophy. (In this particular case, I think they're both
pretty good tradeoffs.)

This is obviously pretty specific to Python - most prototype based OO
languages don't have the option of metaclasses. But someone else said
that prototype-based languages "tend to grow features that mimic
class-based ones", or soemthing like that. I think this is a good
example.

Joe
Jul 18 '05 #7
In article <10************ *@news.supernew s.com>, John Roth wrote:
It's certainly true that in a prototype based language all objects
exist: there are no objects that the compiler deals with but does
not put into the resulting program. And it's quite true that it does
open up the floodgates for a lot of messiness.


Hmm, I bet an optimizing compiler could take care of that for statically
linked programs, although you'd still need to put all the objects in the
libraries just in case some user wants to instantiate them. (Although
an explicit export list could help the compiler out there.)

Joe
Jul 18 '05 #8
On Sun, Mar 28, 2004 at 03:37:25AM +0000, Joe Mason wrote:
[...]

Ed Suominen just posted a situation where classes and instances need to
be treated separately. He has a class, "AutoText", with various
subclasses (which I'll call "AutoA", "AutoB", etc. because I forget the
details). He wants to notice whenever somebody creates a subclass of
AutoText and add it to a list so that he can have an AutoAll function
which creates just one instance of each. (Or something like that.)
[...] In Python, the easiest way to do this is with metaclasses, which is a
really advanced topic. In Prothon, it's simple to "notice" when a
subclass is created, because __init__ gets called! So you just say:


Actually, in Python, it's even easier than that:
class C(object): pass .... class D(C): pass .... C.__subclasses_ _()

[<class '__main__.D'>]

-Andrew.
Jul 18 '05 #9
In article <ma************ *************** *********@pytho n.org>, Andrew Bennetts wrote:
Actually, in Python, it's even easier than that:
class C(object): pass ... class D(C): pass ... C.__subclasses_ _()

[<class '__main__.D'>]


Cool. How come this isn't in either the Language Reference or Library
Reference? I just skimmed through those looking for special
class-related methods the other day, when I first started looking at
prototypes, and didn't notice __subclasses__, and now I can't find it in
the index.

Joe
Jul 18 '05 #10

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

Similar topics

0
1400
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
1312
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...
28
3304
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
7
3563
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...
49
2622
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
1809
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
9072
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
8896
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
9653
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
9451
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
9421
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
9333
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
8328
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...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.