英文:
Return class object after changing object variables
问题
我是新手使用Python中的面向对象编程(OOP),也是新手总体来说,我想知道更改对象变量的正确方法。由于Python不能返回"void",所以当对象变量发生更改时,我需要返回一些东西。
```python
class classA:
var_a = 1
var_b = 1
def changeSomething(classAObj):
classAObj.var_a = 2
return classAObj
def main():
classAObj = classA()
changeSomething(classAObj)
.....在这里使用具有新更改的对象.....
在changeSomething
函数中,将整个classAObj
返回是否正确?还是只返回变量,即
...
return classAobj.var_a
无论哪种方式,我都能获得相同的结果,classAObj.var_a
都发生了变化,但正确的做法是什么?
<details>
<summary>英文:</summary>
Im new to OOP in Python (and in general) and wondering about the correct way to change object variables. Since Python cant return void i need to return something when an object variable has changed.
class classA:
var_a = 1
var_b = 1
def changeSomething(classAObj):
classAObj.var_a = 2
return classAobj
def main()
classAObj = classA()
changeSomething(classAObj)
.....use object with new changes here....
Is it correct to return the entire classAObj in the changeSomething function? Or should i only return the variable i.e
...
return classAobj.var_a
I get the same result in either way, classAObj.var_a has changed in both cases, but what is the correct way to do it?
</details>
# 答案1
**得分**: 0
`changeSomething` 不应该返回*任何东西*;当它直接修改参数时,应该隐式地返回`None`。只有那些生成新修改*副本*的函数应该返回它们(并保持参数不被修改)。
所以你的代码应该是这样的:
```python
class classA:
def __init__(self):
self.var_a = 1 # 这里忘了加self
self.var_b = 1 # 这里忘了加self
def changeSomething(classAObj):
classAObj.var_a = 2 # 修复名称,self不会存在,你可以通过不同的名称接收它
def main():
classAObj = classA()
changeSomething(classAObj) # classAObj在原地修改,所以在调用者看到的是更改
# .....在这里使用具有新更改的对象.....
或者,考虑到这是一个类,将changeSomething
作为实例方法更有意义,例如:
class classA:
def __init__(self):
self.var_a = 1 # 这里忘了加self
self.var_b = 1 # 这里忘了加self
# 缩进以将其设置为类的方法
def changeSomething(self): # 参数重命名以符合实例方法的标准命名
self.var_a = 2
def main():
classAObj = classA()
classAObj.changeSomething() # 使用方法调用语法,classAObj仍在原地修改
# .....在这里使用具有新更改的对象.....
英文:
changeSomething
should not return anything; when it modifies the argument in place, it should return nothing (implicitly None
). Only functions that make new modified copies should return them (and leave the argument unmodified).
So your code should just be:
class classA:
def __init__(self):
self.var_a = 1 # You forget self here
self.var_b = 1 # You forget self here
def changeSomething(classAObj):
classAObj.var_a = 2 # Fix name, self wouldn't exist, you received it by a different name
def main():
classAObj = classA()
changeSomething(classAObj) # classAObj is modified in place, so changes seen in caller
# .....use object with new changes here....
Or, given this is a class, it makes sense to have changeSomething
be an instance method, e.g.:
class classA:
def __init__(self):
self.var_a = 1 # You forget self here
self.var_b = 1 # You forget self here
# Indented to make it a method of the class
def changeSomething(self): # Argument renamed to follow standard naming for instance methods
self.var_a = 2
def main():
classAObj = classA()
classAObj.changeSomething() # Use method call syntax, classAObj still modified in place
# .....use object with new changes here....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论