本文介绍了为什么我们经常使用obj $ func()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Michael Geary <弥** @ Geary.com>写道......




关于委托和绑定已有很多消息,因为Greg

最初发布了他的喵喵猫消息,很难记住是什么

最初的问题是格雷格指出的。那时Prothon没有解决这个问题的解决方案。现在有一个已发布的解决方案,但它已经不完整了,并打破了xor运算符(^)(不要告诉任何人< grin>)。

我'我会以自己的方式在这里重申它(这是从我在comp.lang.py上发布的内容中复制的。


问题陈述(这些是已发布的Python测试程序和

Prothon :):


#Python


class klass:

def __init __(自我):

self.me = 1

def func(self):

print" func1,self" + str(self.me),


class klass2(klass):

def __init __(self):

self.me = 2

def func(self):

klass.func(self)#assition

print" func2, self" + str(self.me),

inst = klass2()

inst.func()#prints func1,self2 func2,self2


#直接翻译成Prothon


Klass = Object()
$ K $ b与Klass:

.me = 0#extra line

def .__ init __():

.me = 1

def .func() :

print" func1,self" +。me,


Klass2 = Klass()

with Klass2:

def .__ init __():

.me = 2

def .func():

Klass.func( )#不做python的工作

print" func2,self" +。me,


inst = Klass2()

inst.func()#打印func1,self0 func2,self2


如你所见,调用Klass.func()得到了来自对象的函数func

Klass然后使用Klass作为目标自我在Klass上调用它。而是

的实例inst。这不是Python所做的,这是

问题。


在Python中,调用klass.func()是不同的,因为Python知道

klass是一个类,因此显然不能成为调用的目标(一个

实例),所以它让程序员通过实例self来用作

参数。


为了解决这个问题,我们需要能够一般性地指定自我对象

在执行期间将会是什么Prothon中的一个函数(我称之为

选择自我绑定自我到函数,即使在执行
期间它是暂时的)。


Greg的解决方案是用另一个符号替换

klass.func()中函数名之前的句号,表示函数不是
应该按照语法通常建议的那样将
绑定到klass(如

list.sort!()),而是默认为当前的self。我的最终方案几乎是相同的,但不仅仅是默认为自我,它特别是

强迫它自我(这可能看起来像语义差异,但不是''对Greg< grin>)说




现在我们有了这个解决方案。请注意,委托给祖先原型

(Python中的类),是一种常见的操作,特别是在__init__

函数中:


Klass = Object()
$ K $ b与Klass:

def $ __ init __():

$ me = 1

def $ func():

print" func1,self" + $ me,


Klass2 = Klass()

与Klass2:

def $ __ init __():

$ me = 2

def $ func():

Klass $ func()#voila!修复问题

打印func2,self+ $ me,

inst = Klass2()

inst.func( )#prote func1,self2 func2,self2

解决方案




自从Greg

最初发布了他的喵喵叫猫的消息以来,已经发布了很多关于委托和绑定的消息很难记住什么

原来的问题是Greg指出的。那时Prothon没有解决这个问题的解决方案。现在有一个已发布的解决方案,但它已经不完整了,并打破了xor运算符(^)(不要告诉任何人< grin>)。

我'我会以自己的方式在这里重申它(这是从我在comp.lang.py上发布的内容中复制的。


问题陈述(这些是已发布的Python测试程序和

Prothon :):


#Python


class klass:

def __init __(自我):

self.me = 1

def func(self):

print" func1,self" + str(self.me),


class klass2(klass):

def __init __(self):

self.me = 2

def func(self):

klass.func(self)#assition

print" func2, self" + str(self.me),

inst = klass2()

inst.func()#prints func1,self2 func2,self2


#直接翻译成Prothon


Klass = Object()
$ K $ b与Klass:

.me = 0#extra line

def .__ init __():

.me = 1

def .func() :

print" func1,self" +。me,


Klass2 = Klass()

with Klass2:

def .__ init __():

.me = 2

def .func():

Klass.func( )#不做python的工作

print" func2,self" +。me,


inst = Klass2()

inst.func()#打印func1,self0 func2,self2


如你所见,调用Klass.func()得到了来自对象的函数func

Klass然后使用Klass作为目标自我在Klass上调用它。而是

的实例inst。这不是Python所做的,这是

问题。


在Python中,调用klass.func()是不同的,因为Python知道

klass是一个类,因此显然不能成为调用的目标(一个

实例),所以它让程序员通过实例self来用作

参数。


为了解决这个问题,我们需要能够一般性地指定自我对象

在执行期间将会是什么Prothon中的一个函数(我称之为

选择自我绑定自我到函数,即使在执行
期间它是暂时的)。


Greg的解决方案是用另一个符号替换

klass.func()中函数名之前的句号,表示函数不是
应该按照语法通常建议的那样将
绑定到klass(如

list.sort!()),而是默认为当前的self。我的最终方案几乎是相同的,但不仅仅是默认为自我,它特别是

强迫它自我(这可能看起来像语义差异,但不是''对Greg< grin>)说




现在我们有了这个解决方案。请注意,委托给祖先原型

(Python中的类),是一种常见的操作,特别是在__init__

函数中:


Klass = Object()
$ K $ b与Klass:

def




"Michael Geary" <Mi**@Geary.com> wrote ...



There have been so many messages about delegation and binding since Greg
originally posted his meowing cat message that it''s hard to remember what
the original problem was that Greg pointed out. At that time Prothon had no
solution for the problem. Now there is a released solution but it is
incomplete and has broken the xor operator ( ^ ) (don''t tell anyone <grin>).
I''ll restate it here in my own way (this is copied from something I posted
on comp.lang.py).

Statement of problem (These are tested programs from released Python and
Prothon:):

# Python

class klass:
def __init__(self):
self.me = 1
def func(self):
print "func1,self"+str(self.me),

class klass2(klass):
def __init__(self):
self.me = 2
def func(self):
klass.func(self) # delegation
print "func2,self"+str(self.me),

inst = klass2()
inst.func() # prints func1,self2 func2,self2

# Directly translated to Prothon

Klass = Object()
with Klass:
.me = 0 # extra line
def .__init__():
.me = 1
def .func():
print "func1,self"+.me,

Klass2 = Klass()
with Klass2:
def .__init__():
.me = 2
def .func():
Klass.func() # does not do what python does
print "func2,self"+.me,

inst = Klass2()
inst.func() # prints func1,self0 func2,self2

As you can see, the call Klass.func() got the function func from the object
Klass and then called it on Klass using Klass as the target "self" instead
of the instance "inst". This is not what Python did and this is the
problem.

In Python the call klass.func() was different because Python knows that
klass is a class and therefore obviously cannot be the target of call (an
instance), so it made the programmer pass the instance self to use as a
parameter.

To fix this, we need to be able to specify in general, what the self object
is going to be during the execution of a function in Prothon (I call this
choice of self "binding self to function" even though it''s temporary during
execution).

Greg''s solution was to replace the period before the function name in
klass.func() with a different symbol to indicate that the func is not
supposed to be bound to klass as the syntax normally suggests (as in
list.sort!()), but instead defaults to the current self. My final scheme is
almost the same but instead of just defaulting to self, it specifically
forces it to self (this may seem like a semantic difference, but don''t say
that to Greg <grin>).

So now we have this solution. Note that delegation to an ancestor prototype
(class in Python), is a common operation, especially in the __init__
function:

Klass = Object()
with Klass:
def $__init__():
$me = 1
def $func():
print "func1,self"+$me,

Klass2 = Klass()
with Klass2:
def $__init__():
$me = 2
def $func():
Klass$func() # voila! problem fixed
print "func2,self"+$me,

inst = Klass2()
inst.func() # prints func1,self2 func2,self2

解决方案



There have been so many messages about delegation and binding since Greg
originally posted his meowing cat message that it''s hard to remember what
the original problem was that Greg pointed out. At that time Prothon had no
solution for the problem. Now there is a released solution but it is
incomplete and has broken the xor operator ( ^ ) (don''t tell anyone <grin>).
I''ll restate it here in my own way (this is copied from something I posted
on comp.lang.py).

Statement of problem (These are tested programs from released Python and
Prothon:):

# Python

class klass:
def __init__(self):
self.me = 1
def func(self):
print "func1,self"+str(self.me),

class klass2(klass):
def __init__(self):
self.me = 2
def func(self):
klass.func(self) # delegation
print "func2,self"+str(self.me),

inst = klass2()
inst.func() # prints func1,self2 func2,self2

# Directly translated to Prothon

Klass = Object()
with Klass:
.me = 0 # extra line
def .__init__():
.me = 1
def .func():
print "func1,self"+.me,

Klass2 = Klass()
with Klass2:
def .__init__():
.me = 2
def .func():
Klass.func() # does not do what python does
print "func2,self"+.me,

inst = Klass2()
inst.func() # prints func1,self0 func2,self2

As you can see, the call Klass.func() got the function func from the object
Klass and then called it on Klass using Klass as the target "self" instead
of the instance "inst". This is not what Python did and this is the
problem.

In Python the call klass.func() was different because Python knows that
klass is a class and therefore obviously cannot be the target of call (an
instance), so it made the programmer pass the instance self to use as a
parameter.

To fix this, we need to be able to specify in general, what the self object
is going to be during the execution of a function in Prothon (I call this
choice of self "binding self to function" even though it''s temporary during
execution).

Greg''s solution was to replace the period before the function name in
klass.func() with a different symbol to indicate that the func is not
supposed to be bound to klass as the syntax normally suggests (as in
list.sort!()), but instead defaults to the current self. My final scheme is
almost the same but instead of just defaulting to self, it specifically
forces it to self (this may seem like a semantic difference, but don''t say
that to Greg <grin>).

So now we have this solution. Note that delegation to an ancestor prototype
(class in Python), is a common operation, especially in the __init__
function:

Klass = Object()
with Klass:
def




这篇关于为什么我们经常使用obj $ func()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 15:01