作者:儒雅的aaaaaaaaaaa | 来源:互联网 | 2023-06-08 13:49
Iwanttobeabletocreateanewinstanceofanobjectbycallingamethodonanalreadyinstantiat
I want to be able to create a new instance of an object by calling a method on an already instantiated object. For example, I have the object:
我希望能够通过调用已实例化的对象上的方法来创建对象的新实例。例如,我有对象:
organism = Organism()
有机体=有机体()
I want to be able to call organism.reproduce()
and have two objects of type Organism. My method at this point looks like this:
我希望能够调用organism.reproduce()并拥有两个类型为Organism的对象。我的方法在这一点看起来像这样:
class Organism(object):
def reproduce():
organism = Organism()
and I'm pretty sure it doesn't work (I'm not really even sure how to test it. I tried the gc method in this post). So how can I make my object create a copy of itself that's accessible just like the first object I created (with organism = Organism()
)?
而且我很确定它不起作用(我甚至不确定如何测试它。我在这篇文章中尝试了gc方法)。那么我怎样才能让我的对象创建一个可以访问的副本,就像我创建的第一个对象一样(使用organism = Organism())?
5 个解决方案
34
class Organism(object):
def reproduce(self):
#use self here to customize the new organism ...
return Organism()
Another option -- if the instance (self
) isn't used within the method:
另一个选项 - 如果在方法中未使用实例(self):
class Organism(object):
@classmethod
def reproduce(cls):
return cls()
This makes sure that Organisms produce more Organisms and (hypothetical Borgs which are derived from Organisms produce more Borgs).
这确保了生物体产生更多的生物体(假设的生物体来源于生物体产生更多的生物体)。
A side benefit of not needing to use self
is that this can now be called from the class directly in addition to being able to be called from an instance:
不需要使用self的附带好处是,除了能够从实例调用之外,现在可以直接从类中调用它:
new_organism0 = Organism.reproduce() # Creates a new organism
new_organism1 = new_organism0.reproduce() # Also creates a new organism
Finally, if both the instance (self
) and the class (Organism
or subclasses if called from a subclass) are used within the method:
最后,如果在方法中使用实例(self)和类(Organism或子类,如果从子类调用):
class Organism(object):
def reproduce(self):
#use self here to customize the new organism ...
return self.__class__() # same as cls = type(self); return cls()
In each case, you'd use it as:
在每种情况下,您都将它用作:
organism = Organism()
new_organism = organism.reproduce()
1
The same way you did originally, but then you have to do something with it!
就像你原来做的那样,但是你必须用它做点什么!
organism = Organism()
calls the class Organism
(parentheses directly after a name is the "call" operation). This creates and returns a new instance of the class, which you then bind to the name organism
.
有机体=有机体()调用类有机体(名称后面的括号是“调用”操作)。这将创建并返回该类的新实例,然后将其绑定到名称有机体。
When you execute that line in the interpreter, you now have a variable organism
referring to the new Organism
instance you just created.
当您在解释器中执行该行时,您现在有一个变量有机体引用您刚创建的新的有机体实例。
When you write that line inside a function (including a method, because there's no difference between a method and a function "from the inside"), it does the same thing, but the variable organism
is a local variable. Local variables are thrown away when the function is finished, so this does create a new Organism
instance, but it doesn't achieve anything because you never gain access to it.
当你在函数中写入那一行时(包括一个方法,因为方法和函数之间没有“内部”),它做同样的事情,但变量有机体是一个局部变量。函数完成时会丢弃局部变量,因此这会创建一个新的Organism实例,但它无法实现任何功能,因为您永远无法访问它。
Your function should return any information it wants to communicate to its caller. Any local variables that you don't return are only useful if you use those variables to create something you do return.
您的函数应该返回它想要与其调用者通信的任何信息。您不返回的任何局部变量仅在您使用这些变量创建返回的内容时才有用。
Note that this has nothing to do with your particular problem of creating an instance inside a method; it's just how functions/methods work in general. You will need to learn how functions work before you can successfully write object-oriented programs using classes and instances; I would strongly suggest you work through some tutorials.
请注意,这与在方法中创建实例的特定问题无关;它只是函数/方法的一般工作方式。在使用类和实例成功编写面向对象的程序之前,您需要了解函数的工作原理;我强烈建议你完成一些教程。