[Learning memo] Basics of class by python

object

Contains data (variables (attributes)) and code (functions (methods))

Class basics

Define the Person class.

python


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

hunter=Person('Reiner Tonnies')
print('The mighty hunter:', hunter.name) # The mighty hunter: Reiner Tonnies

Inheritance

python


Class Car():
    def exclaim(self):
        print("I'm a Car!")

Class Yugo(Car):
    def exclaim(self):
        print("I'm a Yugo!")
    def need_a_push(self):
        print("Hi!")

give_me_a_car=Car()
give_me_a_yugo=Yugo()

#override
give_me_a_car.exclaim() #I'm a Car!
give_me_a_yugo.exclaim() #I'm a Yugo!

#Add method
give_me_a_yugo.need_a_push() #Hi!
give_me_a_car.need_a_push() #error



Recommended Posts

[Learning memo] Basics of class by python
Python class (Python learning memo ⑦)
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Interval scheduling learning memo ~ by python ~
Basics of Python ①
Basics of python ①
#Python basics (class)
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python module (Python learning memo ④)
Visualization memo by Python
Python: Unsupervised Learning: Basics
Basics of Python scraping basics
# 4 [python] Basics of functions
Basics of python: Output
Python & Machine Learning Study Memo ⑤: Classification of irises
Python & Machine Learning Study Memo ②: Introduction of Library
Python learning memo for machine learning by Chainer until the end of Chapter 2
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Basics of Machine Learning (Notes)
Python learning memo for machine learning by Chainer from Chapter 2
Python learning memo for machine learning by Chainer Chapters 1 and 2
python: Basics of using scikit-learn ①
Supervised learning 1 Basics of supervised learning (classification)
Basics of Python learning ~ What is a string literal? ~
Basics of Python × GIS (Part 1)
Memo of "Cython-Speeding up Python by fusing with C"
[For beginners] Basics of Python explained by Java Gold Part 2
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
[For beginners] Basics of Python explained by Java Gold Part 1
Python control syntax, functions (Python learning memo ②)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
[Python] Operation memo of pandas DataFrame
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Expansion by argument of python dictionary
Python: Application of supervised learning (regression)
Machine learning summary by Python beginners
Input / output with Python (Python learning memo ⑤)
Getting Started with Python Basics of Python
Python3 compatible memo of "python start book"
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
Separate display of Python graphs (memo)
Behavior of python3 by Sakura's server
"Scraping & machine learning with Python" Learning memo
About the basics list of Python basics
Story of power approximation by Python
(python) Deep Learning Library Chainer Basics Basics
Learn the basics of Python ① Beginners
[Python] A memo of frequently used phrases (by myself) in Python scripts
Python basics ⑤
Python basics
Python memo
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python basics ④
python memo
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
Python memo
Python basics ③
Python basics