Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new class instance from class method

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())?

like image 505
ohblahitsme Avatar asked Nov 06 '12 22:11

ohblahitsme


People also ask

How do I create a new instance of a method?

In Java, we can create Objects in various ways: Using a new keyword. Using the newInstance () method of the Class class. Using the newInstance() method of the Constructor class.

How do I create a new instance of a class?

Instantiating a ClassThe new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created.

How do you create an instance of a class in a function?

Creating Instance Objects To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.

When a new instance of a class is created which method gets?

Constructors in Python Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated. This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.


1 Answers

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:

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:

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:

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() 
like image 143
mgilson Avatar answered Sep 20 '22 12:09

mgilson