Prothon原型与Python类 [英] Prothon Prototypes vs Python Classes

查看:84
本文介绍了Prothon原型与Python类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天和Prothon一起玩,我对在Python中消除

类的想法很着迷。我正在努力弄清楚什么是基本的好处

有课。所有这些复杂性都是不必要的吗?


这是一个包含所有三种类型方法的Python类的示例

(实例,静态和类方法)。


#例子来自学习Python的第23页,第381-2页,第2版。


class Multi:

numInstances = 0

def __init __(自我):

Multi.numInstances + = 1

def printNumInstances():

print" Instances of Instances:",Multi.numInstances

printNumInstances = staticmethod(printNumInstances)

def cmeth(cls,x):

print cls,x

cmeth = classmethod(cmeth)


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


Multi.printNumInstances()

a.printNumInstances()


Multi.cmeth (5)

b.cmeth(6)

以下是Prothon的翻译。


Multi = Object()

with Multi:

.numInstances = 0

def .__ init __():#instance method

Multi.numInstances + = 1

def .printNumInstances():#静态方法

print" Instances of Instances:",Multi.numInstances

def .cmeth(x):#class方法

打印Multi,x

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


Multi.printNumInstances()

a.printNumInstances()


Multi.cmeth (5)

b.cmeth(6)

注意在这些方法中消除''self''。这不仅仅是一个

语法快捷方式(对''self''取代''''')通过消除这个自我对象的显式传递,Prothon使所有方法形式

相同并消除了很多复杂性。它已经开始看起来很像Python类的复杂性是不必要的。


我对Python专家的问题是 - 我们的用户利益是什么
如果我们消除课程,
会丢失吗?


- 戴夫

解决方案



" David MacQuigg" < dm*@gain.com>在消息中写道

news:56 ******************************** @ 4ax.com ...

今天和Prothon一起玩,我对在Python中消除
类的想法非常着迷。我正在努力弄清楚有什么基本的好处才能上课。所有这些复杂性都是不必要的吗?

这是一个Python类的示例,包含所有三种类型的方法
(实例,静态和类方法)。

#Samples的第23章,第381-2页的例子,第2版。

类Multi:
numInstances = 0
def __init __(self):
Multi.numInstances + = 1
def printNumInstances():
print" Instances of Instances:",Multi.numInstances
printNumInstances = staticmethod(printNumInstances)
def cmeth( cls,x):
print cls,x
cmeth = classmethod(cmeth)

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

Multi.printNumInstances()
a.printNumInstances()

Multi.cmeth(5)
b.cmeth(6 )

以下是Prothon的翻译。

Multi = Object()
with Multi:
.numInstances = 0
def。 __init __():#instance method
Multi.numInstances + = 1
def .printNumInstances():#static method
print" Instances of Instances:",Multi.numInstances def .cmeth(x):#class方法
打印Multi,x
a = Multi(); b = Multi(); c = Multi()

Multi.printNumInstances()
a.printNumInstances()

Multi.cmeth(5)
b.cmeth(6 )

注意在这些方法中消除''self''。这不仅仅是一个语法快捷方式(对''self''取代''''')通过消除这个自我对象的显式传递,Prothon使所有方法形式相同并消除了很多复杂性。它开始看起来像Python类的复杂性是不必要的。


你在这里混合了两个不同的问题。我要去的第一个地址是名称空间。我倾向于同意你的意见,而且我之前已经说过了b $ b。大多数语言都可以通过在词法不同的命名空间中添加不同类型的标识符来获益很多,并且允许编辑人员处理这些问题。我们希望现代编辑能够给关键字着色,为什么不让他们做一些狗狗呢?

我对Python专家的问题是 - 我们的用户利益是什么?如果我们删除课程,会丢失吗?


Duh?再说一次,据我所知,类是一个剩余的想法来自

静态类型。但是,你应该想到很多标准的

OO模式。例如,我们有一些永远不会被实例化的类,以及那些类和/或
只有一个实例(单例)的类。在交互式小说中(而且我怀疑其他应用领域)b / b
一个实例的课程很少见(但并非闻所未闻)。


在更常见的应用程序中,您通常会有大量任何给定类的

实例;这些实例的行为没有变化,所以通过区分类和实例之间的
来优化访问是有意义的。事实上,这就是Python

的作用。它具有语法支持,用于创建类型为class的实例,

完整内置支持方法和花式初始化

选项,以及非常简短的语法流行

创建类型为instance的实例的操作。


我已经看到了另一个最近的评论,消除了

类将为

更大的程序删除非常有价值的抽象级别。这个想法很有吸引力。我想看看

基于原型语言的大型系统示例

以及它们如何处理问题,或者确实是一个问题。


我非常怀疑,如果我要找到一个极端或

其他是正确的。它似乎与你想要做的不同

用它。


John Roth
- 戴夫



我对原型的概念并不十分熟悉,虽然我相信我理解其基本含义并且我使用的是语言

哪些使用原型(虽然没有称之为)。


Aren不是很多次因为使用它们而无法使用

的课程你不希望实例存在?比如多级

子类,通过将代码放在单独的该函数的

适当类上,可以轻松读取代码。当你使用许多相似但不相同的物体时,你常常会有用吗?我可以看到如何能够在一个对象上定义新方法,或者在一个对象上编辑

方法,但是我看不到删除课程的目的

。我的印象是正确的,即使用原型设计,你必须立即对象来定义它的结构吗?这看起来很浪费

和命名空间的混乱。另外,他的原型读取方式似乎比我的类定义更清楚了。我担心你会结束创建一个对象的人们......编码随机的东西......添加一个方法来对付这个对象..编码更多随机的东西。然后在

对象中添加更多内容。




" Michael" <莫***** @ mlug.missouri.edu>在留言中写道

news:40 ************** @ mlug.missouri.edu ...

我不是非常熟悉原型的概念,虽然我相信我理解其基本含义,并且我使用的语言
使用原型(虽然没有称之为)。

Aren'有很多次,当你不想让一个实例存在时,有什么方法可以使用它们吗?比如多级
子类,通过将代码放在单独的该函数的适当类上,可以轻松读取代码。当你使用许多相似但不相同的物体时常常有用吗?我可以看到如何能够在对象上定义新方法,或者在对象上编辑
方法,但是我看不到删除类的目的
共。我的印象是正确的,通过原型设计,您必须立即对象来定义它的结构吗?这看起来很浪费,而且命名空间也很混乱。另外,他的原型阅读方式对我来说不像一个类定义那么清晰。我担心你会结束创建一个对象的人...编码随机的东西..添加一个方法来对象...编码更随机的东西..然后添加更多的
对象。




在基于原型的语言中确实存在所有对象

确实存在:没有对象编译器处理但是没有将
放入生成的程序中。并且确实如此。

打开闸门以获得很多混乱。


另一方面,有应用领域那就是你需要的b $ b:我已经提到了互动小说,

,毫无疑问是其他人。


我想玩一个基于原型的语言来看看它是如何工作的,虽然由于我在其他地方提到的原因,我不会使用

Prothon。如果他们有一个Windows可执行安装程序,我会使用IO,

而不是要求我编译愚蠢的东西。


John Roth


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.numInstances += 1
def printNumInstances():
print "Number of Instances:", Multi.numInstances
printNumInstances = staticmethod(printNumInstances)
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cmeth)

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

Multi.printNumInstances()
a.printNumInstances()

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

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

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

Multi.printNumInstances()
a.printNumInstances()

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

解决方案


"David MacQuigg" <dm*@gain.com> wrote in message
news:56********************************@4ax.com...

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.numInstances += 1
def printNumInstances():
print "Number of Instances:", Multi.numInstances
printNumInstances = staticmethod(printNumInstances)
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cmeth)

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

Multi.printNumInstances()
a.printNumInstances()

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

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

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

Multi.printNumInstances()
a.printNumInstances()

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



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.



"Michael" <mo*****@mlug.missouri.edu> wrote in message
news:40**************@mlug.missouri.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


这篇关于Prothon原型与Python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆