Python classes and instances, instance methods

I couldn't catch up with my understanding in Python Learning Course IV Leave a study note while reviewing. If it's the same code, it's boring and I can't remember it I will write it with the game material of my favorite "CLOCK ZERO ~ Shuuen no Ichibyo ~". spoiler alert.


** Create a class **

Class is defined by "class class name:" Class names start with a capital letter

class CZmember:

Write pass when there is nothing in the class Align the indent

class CZmember:
  pass

There is no processing, that is, it is still before the fall of 2010.


** Instantiate from class **

You can create a new instance by calling "class ()" and that class The class generated by "variable name = class ()" can be assigned to the variable

Generate an empty class

class CZmember:
   pass

cz_member1 = CZmember()

Add information to your instance This time, let's use the name and age of the CZ member.

class CZmember:
   pass

#Create an instance
cz_member1 = CZmember()

#Add information to your instance
cz_member1.name = 'Takato'
cz_member1.age  = 12

print(cz_member1.name + 'Is' + str(cz_member1.age) +I'm old)

Takato is 12 years old

This "name" and "age" part is called an instance variable. What is the target of the game for elementary school students?

Members will be added quickly too!

class CZmember:
   pass

cz_member0 = CZmember()
cz_member0.name = 'Nadeshiko'
cz_member0.age  = 12

cz_member1 = CZmember()
cz_member1.name = 'Takato'
cz_member1.age  = 12

cz_member2 = CZmember()
cz_member2.name = 'Riichiro'
cz_member2.age  = 11

Even though there are 7 members, it's annoying to write like this. So let's add it in the item that will appear later.


** Add processing inside the class **

(Continued from time to time)


"CLOCK ZERO ~ One Second of the End ~" Because I can cry so much with the game released on Switch If you are interested, please play it.

Recommended Posts

Python classes and instances, instance methods
Bind methods to Python classes and instances
How python classes and magic methods are working.
Python: Class and instance variables
About python objects and classes
Python class variables and instance variables
Python ABC-Abstract Classes and Duck Typing
Python class definitions and instance handling
Private methods and fields in python [encryption]
Talking about old and new Python classes
[Python] class, instance
Python is instance
Python class, instance
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Organize the meaning of methods, classes and objects
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
[Python] How to sort instances by instance variables
[python] Compress and decompress
About Class and Instance
Python and numpy tips
The simplest Python memo in Japan (classes and objects)
[Python] pip and wheel
Batch design and python
Convenient Python methods, etc.
[python] class basic methods
Python packages and modules
Vue-Cli and Python integration
Python classes are slow
Ruby, Python and map
python input and output
The abstract methods of python's abc module make no distinction between classes and instances.
Python and Ruby split
[Python] How to sort dict in list and instance in list
Metaclass and is instance
Python basic course (13 classes)
Class methods and static methods
Python3, venv and Ansible
Python asyncio and ContextVar
[Python] Types of statistical values (features) and calculation methods
List method argument information for classes and modules in Python
Foreigners talk: How to name classes and methods in English
Reference order of class variables and instance variables in "self. Class variables" in Python
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Programming with Python and Tkinter
Encryption and decryption with Python
Various class methods and static methods
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
String object methods in Python
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo