An Introduction to Object-Oriented-Give an object a child.

I am writing an article as a memo while making parts for evolution simulation by Python. The previous article was here (Introduction to Object Orientation-Let's change the internal state of an object)

This time, I made a draft of a method (reproduction) that allows a virtual individual to give birth. The reproduction method uses deepcopy of the copy module.

reproduction.py


#Implementation of reproduction
import copy                     #Use deepcopy for reproduction.

class SampleIndividual:
    def __init__(self,x,y,z):
            self.state_x = x
            self.state_y = y
            self.state_z = z

    def mutation(self):                     #Method for mutation of internal state.
        self.state_x = 10

    def reproduction(self):                 #Method to give birth to a child
        self.child = copy.deepcopy(self)    
        self.child.mutation()               #Mutation of internal state
        return self.child

Below is the output. Properly mother and child are separate instances.

output


mother = SampleIndividual(5,5,5)        #Creating a parent

child = mother.reproduction()           #Creating a child
print(vars(child))
#=> {'state_x': 10, 'state_y': 5, 'state_z': 5}

print("mother id:{}".format(id(mother)))             #Check if it is another instance
print("child id:{}".format(id(child)))
#=> 4453353808
#=> 4453466064

that's all. Thank you for reading.

Recommended Posts

An Introduction to Object-Oriented-Give an object a child.
A light introduction to object detection
A quick introduction to pytest-mock
An introduction to machine learning
An introduction to Python Programming
An introduction to Bayesian optimization
An introduction to machine learning from a simple perceptron
An introduction to object orientation-let's change the internal state of an object
Convert a string to an image
An introduction to Python for non-engineers
[Python Tutorial] An Easy Introduction to Python
A super introduction to Python bit operations
An introduction to OpenCV for machine learning
Recurrent Neural Networks: An Introduction to RNN
An introduction to Python for machine learning
An introduction to Python for C programmers
An introduction to self-made Python web applications for a sluggish third-year web engineer
Introduction to MQTT (Introduction)
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
An introduction to object-oriented programming for beginners by beginners
Introduction to Tkinter 1: Introduction
How to create a function object from a string
An introduction to Cython that doesn't go deep
Introduction to PyQt
Introduction to Scrapy (2)
An introduction to statistical modeling for data analysis
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Linux] Introduction to Linux
I tried to detect an object with M2Det!
Introduction to Scrapy (4)
Save the object to a file with pickle
Introduction to Python "Re" 1 Building an execution environment
Introduction to discord.py (2)
An introduction to voice analysis for music apps
Introduction to discord.py
An introduction to Cython that doesn't go deep -2-
[Python] You can save an object to a file by using the pickle module.
Convert a slice object to a list of index numbers
[Blender x Python] How to create an original object
How to convert a class object to a dictionary with SQLAlchemy
An introduction to Python distributed parallel processing with Ray
Reading Note: An Introduction to Data Analysis with Python
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
How to turn a .py file into an .exe file
An introduction to Word2Vec that even cats can understand
How to deploy a Go application to an ECS instance
Python: Get a list of methods for an object
Introduction to Linear Algebra in Python: A = LU Decomposition
Create a shape on the trajectory of an object
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
How to convert an array to a dictionary with Python [Application]