[英]Basic question about python classes and variables from a new programmer
I have a simple question about variable values being updated, please.我有一个关于正在更新的变量值的简单问题,请。 I ran the following code:
我运行了以下代码:
class Number:
def __init__(self,val):
self.val=val
def addthistoobj(self,obj):
obj.val=self.val+obj.val
def addthistovar(self,var):
var=self.val+var
x=Number(7)
print("value of x is",x.val)
y=Number(10)
print("value of y (obj) is",y.val)
x.addthistoobj(y)
print("value of y (obj) after adding is",y.val)
p=10
print("value of p (var) is",p)
x.addthistovar(p)
print("value of p (var) after adding is",p)
The output I got was:我得到的输出是:
" ”
value of x is 7 x 的值为 7
value of y (obj) is 10 y (obj) 的值为 10
value of y(obj) after adding is 17添加后 y(obj) 的值为 17
value of p (var) is 10 p (var) 的值为 10
value of p (var) after adding is 10添加后 p (var) 的值为 10
" ”
The value of the object gets updated but the value of the variable does not.对象的值会更新,但变量的值不会。
Why is this the case?为什么会这样?
Thank you for your time!感谢您的时间!
When you do x.addthistovar(p)
, you are adding the value of x
to p
and creating a new object called var, but it "dies off" as the scope of the addthistovar
function closes.当您执行
x.addthistovar(p)
,您将x
的值添加到p
并创建一个名为 var 的新对象,但它随着addthistovar
函数的作用域关闭而“消失”。
If you wanted to have p == 17
after that, you would need a return statement.如果你想在这之后
p == 17
,你需要一个 return 语句。
class Number:
...
def addthistovar(self,var):
return self.val + var
...
p = x.addthistovar(p)
Change your function to return the sum.更改您的函数以返回总和。 As the value you are passing is not related to that object, thus can't be saved unless you define a field for it.
由于您传递的值与该对象无关,因此除非您为其定义字段,否则无法保存。 You can save the result in a variable
result
and use it to your print
statment.您可以将结果保存在可变
result
并将其用于print
语句。
class Number:
def __init__(self,val):
self.val=val
def addthistoobj(self,obj):
obj.val=self.val+obj.val
def addthistovar(self,var):
return self.val+var
x=Number(7)
print("value of x is",x.val)
y=Number(10)
print("value of y (obj) is",y.val)
x.addthistoobj(y)
print("value of y (obj) after adding is",y.val)
p=10
print("value of p (var) is",p)
result = x.addthistovar(p)
print("value of p (var) after adding is", result)
This is because p
never gets modified.这是因为
p
永远不会被修改。 Let's look at a more simple example:让我们看一个更简单的例子:
def add_to(x, y):
x = x + y
a, b = 1, 2
add_to(a, b)
This might lead you to believe that x
will be re-assigned during the call to add_to
.这可能会让您相信
x
将在调用add_to
期间重新分配。 That's not the case.事实并非如此。
x
in the scope of add_to
becomes a different object, because the +
operator for ints returns a new int. x
中的范围add_to
变为一个不同的对象,这是因为+
为整数运算符返回新的int。 It also gets garbage collected (disappears) at the end of the scope of add_to
.它还会在
add_to
范围的末尾收集(消失)垃圾。 To reassign a
, you would first need to return the new value, and re-assign it:要重新分配
a
,您首先需要返回新值,然后重新分配它:
def add_to(x, y):
return x + y # this returns a new integer created by x + y
a, b = 1, 2
a = add_to(a, b) # this reassigns the value 3 to a
Now for your class, you'd need to do the same thing:现在对于你的班级,你需要做同样的事情:
class Number:
def __init__(self,val):
self.val=val
def addthistoobj(self,obj):
obj.val=self.val+obj.val
def addthistovar(self,var):
return self.val+var
p = 10
x = Number(7)
p = x.addthistovar(p)
p
17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.