Python basics ④

Basic knowledge of Python ④. It is my study memo. Please do not excessive expectations.

Past posts

Python basics Python basics ② Python basics ③


Class definition

-Mechanism to generate "things" In order to generate the "thing", it is first necessary to prepare the "blueprint". The blueprint is class, "Things" are called instances.

Create an instance

1, "Prepare a class" --Class is defined by " class class name: "

Example


class Vehicletype: #Colon required at end of line
    pass #Indicates that there is no processing
    #Align the indents (4 half-width spaces)

2, "Create an instance from a class" -With " class name () " and calling that class You can create a new instance using the class. Then, by setting "variable name = class name ()", the created instance can be assigned to the variable.

Example


class Vehicletype: #Colon required at end of line
    pass #Indicates that there is no processing
    #Align the indents (4 half-width spaces)

vehicle_type1 = Vehicletype()
               #Instantiate from Vehicletype class

3, add information to the instance

-By setting "vehicle_type1.name =" track'", it becomes menu_item1 You can add information that the "name" is a "track". At this time, "name" is called " instance variable ".

Example


class Vehicletype: #Colon required at end of line
    pass #Indicates that there is no processing
    #Align the indents (4 half-width spaces)

vehicle_type1 = Vehicletype()
               #Instantiate from Vehicletype class
vehicle_type1.name = 'truck'

print(vehicle_type1.name) 
#Output result → Track


vehicle_type2 = Vehicletype()
               #Instantiate from Vehicletype class
vehicle_type2.name = 'bus'

print(vehicle_type2.name) 
#Output result → Bus

Add processing in class

-Functions can be defined in the class. The function defined in the class is called method.

-How to define a method You need to add self to the first argument

Example


class Vehicletype: 
    def hello(self): #Add self to the first argument
    #Align the indents (4 half-width spaces)
      print('Hello')

-Call the method defined in the class You can call that method by setting `" instance.method name () "`.

Example


class Vehicletype: 
    def hello(self): #Add self to the first argument
    #Align the indents (4 half-width spaces)
        print('Hello')
        #Align the indents (4 half-width spaces)

vehicle_type1 = Vehicletype()
               #Instantiate from Vehicletype class
vehicle_type1.hello()

#Output result → Hello

Instance method

-The instance that called the method is assigned to "self" specified in the first argument of the instance method. Therefore, by setting "self.name", the value of "name" of "vehicle_type1" that calls the method can be obtained.

Example


class Vehicletype:
    def info(self): #self to vehicle_type1 is assigned
        print(self.name)

vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'

vehicle_type1.info 
#Output result → Track

-Can be returned as a return value using "return"

Example


class Vehicletype:
    def info(self): #self to vehicle_type1 is assigned
        return self.name

vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'

print(vehicle_type1.info )
#Output result → Track

-Instance method (argument)

Example


class Vehicletype:
    def info(self, count): #"4" is assigned to count, the order of the arguments shifts by the amount of "self"

vehicle_type1 = Vehicletype()
vehicle_type1.name = 'truck'

vehicle_type1.info(4)

-__ init__ method The __init__ method is a method that is automatically called immediately after creating an instance with" class name () ".

Example


class Vehicletype:
    def __init__(self):
        print('Please select a car model')

vehicle_type1 = Vehicletype()
                #Since the instance was created__init__The method is called automatically

#Output result → Please select a model

-`__ init__ method` (assign value) You can assign a value to an instance variable at the same time you create an instance.

Example


class Vehicletype:
    def __init__(self):
        self.name = 'truck'

vehicle_type1 = Vehicletype()
                #Since the instance was created__init__The method is called automatically

print(vehicle_type1.name)
#Output result → Track

-__ init__ method (argument) Like instance methods, it can also take arguments.

Example


class Vehicletype:
    def __init__(self, name):
        self.name = name

vehicle_type1 = Vehicletype('truck')
                #Since the instance was created__init__The method is called automatically

print(vehicle_type1.name)
#Output result → Track

Recommended Posts

Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Python: Unsupervised Learning: Basics
Python
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
SNS Python basics made with Flask
Python service
python tips
python function ①
ufo-> python (3)
Python comprehension
install python
Python Singleton
NumPy basics