Python class, instance

Classes and instances

Relationship between class and instance

A class is a blueprint for creating a thing, and an instance is that thing.

Steps to create an instance

Prepare a class ⇨ Create an instance from a class ⇨ Add information to an instance

Class definition

** class Class name: ** The contents of the class are indented in the line after "class class name:" Write ** pass ** if you don't need to add any processing

Create an instance

You can call a class with ** class name () ** and create a new instance using the class By setting ** variable name = class name () **, the generated instance can be assigned to the variable.

Give information to the instance

Add information that name is banana to fruit1 by setting fruit.name = "banana" as shown in the code below. The name at this time is called ** instance variable ** By setting ** instance.instance variable name **, the value of that instance variable can be used.

python.py


class Fruits:
  pass

fruit1 = Fruits()
fruit1.name = "banana"

Add processing in class

You can define a function in a class (this function is called ** method **) The method definition method is the same as a normal function, but you need to add self to the first argument. The method defined in the class is called to be used for the instance. Specifically, the method can be called by setting ** instance.method name () **. The instance that called the method is assigned to "self" specified in the first argument of the instance method. Instances have "instance variables" as information and "instance methods" as processing

init method

The init method can be defined like any other instance and will be called automatically immediately after the instance is created. You can use this method to create an instance and assign a value to an instance variable at the same time. You can also pass arguments and change the value for each instance

Load the class as is

By setting ** from module name import class name **, the specified class in the module can be read directly.

Recommended Posts

[Python] class, instance
Python class, instance
Python: Class and instance variables
Python class variables and instance variables
About python, class
Python class definitions and instance handling
Python is instance
#Python basics (class)
About Class and Instance
Python class (Python learning memo ⑦)
case class in python
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
python subprocess wrapper class
YOLO Python wrapper class
Class notation in Python
Python exception class list
Python
C / C ++ programmer challenges Python (class)
Globalization of class instance variables
class
Python class member scope summary
class
Reference order of class variables and instance variables in "self. Class variables" in Python
How to write a Python class
Use instance method and class method properly
perl objects and python class part 2.
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Landmines hidden in Python class variables
"The easiest Python introductory class" modified
Read PNG chunks in Python (class)
[Python] Road to snake charmer (3) Python class
Examine the object's class in python
perl objects and python class part 1.
[Python] Inherit a class with class variables
Python classes and instances, instance methods
Create an instance of a predefined class from a string in Python
[Road to Python Intermediate] Call a class instance like a function with __call__
I was addicted to confusing class variables and instance variables in Python
I thought a Python class variable was an instance variable and died
kafka python
Generate a first class collection in Python
Python basics ⑤
python + lottery 6
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
Python Summary
Built-in python
Python comprehension
Python technique
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
Implement __eq__ etc. generically in Python class
python tips
python function ①
Python basics
Python memo
[Learning memo] Basics of class by python