[PYTHON] An introduction to object orientation-let's change the internal state of an object

I wanted to use object-oriented programming in evolution simulation, so I wrote a memo. About changing instance variables by method

sample.py


class SampleClass:
    def __init__(self):
        self.internal_state = True  #Internal state(internal_state)Set to True

    def off_internal_state(self):   #Method to set internal state to False
        self.internal_state = False 
    
    def on_internal_state(self):    #Method to set internal state to True
        self.internal_state = True
    
    def change_internal_state(self): #Method to change internal state
        if self.internal_state:         #False if true, True if False
            self.internal_state = False 
        else:
            self.internal_state = True

You now have a SampleClass class with an internal state. Let's look at the output using a method.

sample.py


sampleObj = SampleClass()

print(sampleObj.internal_state)     #=>True

sampleObj.off_internal_state()
print(sampleObj.internal_state)     #=>False

sampleObj.change_internal_state()
print(sampleObj.internal_state)     #=>True

that's all.

Recommended Posts

An introduction to object orientation-let's change the internal state of an object
How to know the internal structure of an object in Python
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
Get the attributes of an object
An Introduction to Object-Oriented-Give an object a child.
Python Note: When you want to know the attributes of an object
Change the decimal point of logging from, to.
From the introduction of pyethapp to the execution of contract
Change the Key of Object on S3 from normal date format to Hive format
I want to store the result of% time, %% time, etc. in an object (variable)
An introduction to data analysis using Python-To increase the number of video views-
[Introduction to Python] Basic usage of the library matplotlib
Roughly estimate the total memory usage of an object
Create a shape on the trajectory of an object
[Blender] How to get the selection order of vertices, edges and faces of an object
An introduction to object orientation-let's change the internal state of an object
Get the attributes of an object
The story of an error in PyOCR
The story of making Python an exe
The story of making an immutable mold
An introduction to data analysis using Python-To increase the number of video views-
From the introduction of pyethapp to the execution of contract
An introduction to private TensorFlow
An introduction to machine learning
[Introduction to cx_Oracle] Overview of cx_Oracle
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
How to change the MAC address of an automatically created virtual NIC to be event driven
Change the volume of Pepper according to the surrounding environment (sound)
Change the theme of Jupyter
An introduction to the modern socket API to learn in C
Change the style of matplotlib
An introduction to Python Programming
An introduction to Bayesian optimization
How to change the log level of Azure SDK for Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
How to change the color of just the button pressed in Tkinter
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Introduction to Python] I compared the naming conventions of C # and Python.
Change the Y-axis scale of Matplotlib to exponential notation (10 Nth power notation)
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
Feel free to change the label of the legend in Seaborn in python
[Go] Create a CLI command to change the extension of the image
I summarized how to change the boot parameters of GRUB and GRUB2
[Pythonista] The story of making an action to copy selected text
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
[Introduction to SIR model] Consider the fitting result of Diamond Princess ♬
An introduction to Mercurial for non-engineers
Change the Flyweight pattern to Pythonic (?) (3)
Change the Flyweight pattern to Pythonic (?) (2)
Change the background of Ubuntu (GNOME)
Change the Python version of Homebrew
Change the Flyweight pattern to Pythonic (?) (1)
Touch the object of the neural network
Change the suffix of django-filter / DateFromToRangeFilter
A light introduction to object detection
The meaning of ".object" in Django
An introduction to Python for non-engineers
Supplement to the explanation of vscode
[Python] Change the alphabet to numbers
[Python Tutorial] An Easy Introduction to Python
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
Various methods to numerically create the inverse function of a certain function Introduction
[Introduction to Python] How to sort the contents of a list efficiently with list sort
When you want to change the HTTP headers of Flask's test client
I tried to summarize the logical way of thinking about object orientation.
__Getattr__ and __getattribute__ to customize the acquisition of object attributes by dots
I want to leave an arbitrary command in the command history of Shell