错误或功能? [英] Bug or feature?

查看:54
本文介绍了错误或功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。在写一些对象时发现了一个奇怪的python行为。我已经把b
剥离了所有其他的东西所以问题就在这里:

================== ===============

#!/ usr / bin / python


class对象:

def __init __(self,val):

self.val = val


def Sub(self,delta):

self.val- = delta

返回delta


c =对象(1)


## #case 1 ###

d = c.Sub(1)

c.val + = d

print''case A:'' ,c.val


###案例2 ###

c.val + = c.Sub(1)

打印''案例B:'',c.val

============================== ===

案例1和案例2打印出不同的校准(分别为1和2)。

不知道 - 如果这是python的bug或功能。


-

Alexey Nezhdanov

Hello. Found a strange python behaivoir while writing some object. I''ve
stripped out all other stuff so the problem is stright here:
=================================
#!/usr/bin/python

class Object:
def __init__(self,val):
self.val=val

def Sub(self,delta):
self.val-=delta
return delta

c=Object(1)

### case 1 ###
d=c.Sub(1)
c.val+=d
print ''case A:'',c.val

### case 2 ###
c.val+=c.Sub(1)
print ''case B:'',c.val
=================================
Case 1 and case 2 prints out different calues (1 and 2 respectively).
Do not know - if this is a bug or feature of python.

--
Alexey Nezhdanov

推荐答案

周四2004年1月15日上午5:40,Alexey Nezhdanov写道:
On Thursday 15 January 2004 5:40 am, Alexey Nezhdanov wrote:
你好。在写一些对象时发现了一个奇怪的python行为。我已经把所有其他东西都剥掉了所以问题就在这里:
========================== =======
#!/ usr / bin / python

类对象:
def __init __(self,val):
self.val = val

def Sub(self,delta):
self.val- = delta
返回delta

c =对象(1)

###案例1 ###
d = c.Sub(1)
c.val + = d
print''案例A:'',c.val

###案例2 ###
c.val + = c.Sub(1)
print''案例B:'',c.val
=================================
案例1和案例2打印出不同的卡路里(1和2)不知道 - 如果这是python的bug或功能。

-
Alexey Nezhdanov
Hello. Found a strange python behaivoir while writing some object. I''ve
stripped out all other stuff so the problem is stright here:
=================================
#!/usr/bin/python

class Object:
def __init__(self,val):
self.val=val

def Sub(self,delta):
self.val-=delta
return delta

c=Object(1)

### case 1 ###
d=c.Sub(1)
c.val+=d
print ''case A:'',c.val

### case 2 ###
c.val+=c.Sub(1)
print ''case B:'',c.val
=================================
Case 1 and case 2 prints out different calues (1 and 2 respectively).
Do not know - if this is a bug or feature of python.

--
Alexey Nezhdanov




这是预期的行为。


RHS上的条款(案例2中的c.val和c.Sub(1))被评估为

在加入之前。如果你颠倒了案例2和

的条款:


c.val = c.Sub(1)+ c.val


c.val将最终为1.


James



This is the expected behaviour.

The terms on the RHS (c.val and c.Sub(1) in case 2) are evaluated left to
right before being added together. If you''d reversed the terms of case 2 and
written:

c.val = c.Sub(1) + c.val

c.val would end up as 1.

James


Alexey Nezhdanov这条鱼在1月14日星期三给企鹅带来了

2004 21:40 pm:
Alexey Nezhdanov fed this fish to the penguins on Wednesday 14 January
2004 21:40 pm:
案例1和案例2打印出不同的卡路里(分别为1和2) 。
不知道 - 如果这是python的bug或功能。

这是使用我认为副作用的问题......而且

不理解变量不是内存地址。


观察 - 我已经明确地将所有var变为var访问

类的方法,这意味着方程必须以长格式写出:


class O:

def __init __(self,ival):

self._val = ival

print" __ init __" ;, self._val


def Sub(self,delta):

self._val - = delta

print" Sub",self._val

return delta


def GetVal(个体经营):

print" GetVal",self._val

return self._val


def SetVal(self,nval):

self._val = nval

print" SetVal",self._val


c = O(1)


打印c.GetVal()

打印


d = c.Sub(1)

c.SetVal(c.GetVal()+ d)


print c.GetVal()

打印


c.SetVal(c.GetVal()+ c.Sub(1))


print c.GetVal ()

打印


c.SetVal(c.Sub(1)+ c.GetVal())


print c.GetVal()

打印


这是运行的结果:


[wulfraed @ beastie wulfraed]
Case 1 and case 2 prints out different calues (1 and 2 respectively).
Do not know - if this is a bug or feature of python.
It''s a problem with using what I would consider a side effect... And
not understanding that "variables" are not memory addresses.

Observe -- I''ve explicitly made all "var" accesses methods of the
class, which means the equations have to be written in long form:

class O:
def __init__(self, ival):
self._val = ival
print "__init__", self._val

def Sub(self, delta):
self._val -= delta
print "Sub", self._val
return delta

def GetVal(self):
print "GetVal", self._val
return self._val

def SetVal(self, nval):
self._val = nval
print "SetVal", self._val

c = O(1)

print c.GetVal()
print

d = c.Sub(1)
c.SetVal(c.GetVal() + d)

print c.GetVal()
print

c.SetVal(c.GetVal() + c.Sub(1))

print c.GetVal()
print

c.SetVal(c.Sub(1) + c.GetVal())

print c.GetVal()
print

And here is the result of the run:

[wulfraed@beastie wulfraed]

python t.py

__init__ 1

GetVal 1

1


Sub 0

GetVal 0

SetVal 1

GetVal 1

1


GetVal 1

Sub 0

SetVal 2

GetVal 2

2

Sub 1

GetVal 1

SetVal 2

GetVal 2

2


注意Sub()和GetVal()调用的顺序如何影响结果。

-
Alexey Nezhdanov


- ============================================ ====== ============<
wl **** *@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
wu******@dm.net | Bestiaria支持人员<
========================================= ========= ============<
Bestiaria主页: http://www.beastie.dm.net/ <
主页: http://www.dm.net/~wulfraed/ <
python t.py
__init__ 1
GetVal 1
1

Sub 0
GetVal 0
SetVal 1
GetVal 1
1

GetVal 1
Sub 0
SetVal 2
GetVal 2
2

Sub 1
GetVal 1
SetVal 2
GetVal 2
2

Note how the order of the Sub() and GetVal() calls affects the result.
--
Alexey Nezhdanov

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <






这篇关于错误或功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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