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:
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())?
Another option — if the instance (
self) isn’t used within the method: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
selfis that this can now be called from the class directly in addition to being able to be called from an instance:Finally, if both the instance (
self) and the class (Organismor subclasses if called from a subclass) are used within the method:In each case, you’d use it as: