简体   繁体   English

如何从python中的类中的方法调用该类以重新定义变量?

[英]how to call a the class from a method within the class in python to redefine a variable?

I'm having some issues redefining an object that I created by using a method within the same class that defines such object. 我在使用定义此类对象的同一类中的方法重新定义我创建的对象时遇到了一些问题。 I have written a small python example. 我写了一个小的python示例。 Here I'm trying to create an object called dog3 using a method in the class dog that calls itself in order to change its dog3.getDogName. 在这里,我试图使用dog类中的一种方法来创建一个名为dog3的对象,该对象会自行调用以更改其dog3.getDogName。 When I print the dog3 name, the call to init did not took effect. 当我打印dog3名称时,对init的调用未生效。 Does anyone knows how to perform this operation? 有谁知道如何执行此操作?

I expect to get an output as 我希望得到一个输出

woofy max bear woofy最大熊

but instead of bear I get woofy again. 但是我不再忍受熊了。

import sys


class Dog():
    def __init__(self, name = 'woofy'):
        self.name = name

    def getDogName(self):
        return self.name

    def newDog(self, new_name):
        return Dog(new_name)


class Animals():
    def __init__(self, *args):

        dog1 = Dog()
        print(dog1.getDogName())

        dog2 = Dog('max')
        print(dog2.getDogName())

        dog3 = dog1
        dog3.newDog('bear')
        print(dog3.getDogName())


def mainApp(args):
    global app 
    app = Animals(args)

if __name__ == "__main__":
    mainApp(sys.argv)

I'm sure an experience python programer would know how to do an operation like this. 我确信经验丰富的python程序员会知道如何执行这样的操作。

Thank you for your help in advance. 提前谢谢你的帮助。

Your code have defined the method newDog to return a new instance of Dog . 您的代码已定义方法newDog以返回Dog的新实例。

Your code also have dog3 being assigned an instance of Dog , but when your code called dog3.newDog(...) the return value is not assigned to anything. 您的代码还为dog3分配了Dog的实例,但是当您的代码称为dog3.newDog(...) ,返回值未分配给任何东西。 so the new Dog instance that got created went nowhere. 因此创建的新Dog实例无处可去。

You might want to consider doing this instead. 您可能要考虑这样做。

    dog3 = dog1.newDog('bear')
    print(dog3.getDogName())

newDog is creating a new dog and not modifying the old one newDog正在创建一只新狗,而不修改旧狗

If you want newDog to return a new dog, then do this: 如果要newDog退还一条新狗,请执行以下操作:

dog3 = dog1.newDog("bear")

or really you should just be doing 还是真的你应该在做

dog3 = Dog("bear")

If you want newDog to modify the current Dog instance, do this: 如果要newDog修改当前的Dog实例,请执行以下操作:

def renameDog(self, new_name):
    self.name = new_name

Don't make instance constructors unless you want to clone certain parameters. 除非要克隆某些参数,否则不要创建实例构造函数。 It can get confusing. 可能会造成混乱。

The method NewDog "return" a new Dog instance, but it will not change what it is, so if u want to change the name of current Dog instance, do this: NewDog方法“返回”一个新的Dog实例,但是它不会改变它的名称,因此,如果您想更改当前Dog实例的名称,请执行以下操作:

class Dog():
    def change_name(self, new_name):
        self.name = new_name

if u want to get another Dog instance, do this: 如果您想获取另一个Dog实例,请执行以下操作:

class Dog(object):
  @staticmethod
  def new_dog(new_name):
      return Dog(new_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM