返回更改对象变量后的类对象

huangapple go评论66阅读模式
英文:

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&#39;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....

huangapple
  • 本文由 发表于 2023年2月10日 07:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定