A way to understand Python duck typing

Overview

I've been looking at Python for a year or two, but I haven't got duck typing in my head. I thought of a way to understand.

By the way, Maybe it's because of the textbook I'm using that I don't have in my head. A little dissed article ↓.

Good book "Introduction to Python3", N selections where explanations have failed (N = 3).

Means to understand

it's simple. There are two steps.

Step 1, write the code for an example that you think is duck typing.

Below is the code I wrote. If you can add a minus to the score, -60 (minus 60 points).

sales point,

Why did you write this code? That's because I remembered that duck typing was a matter of course without any features.

class Tori():#bird
    def tobe(self):
        print("bata-bata")
    def nake(self):
        print("ga-ga")

class Duck(Tori):#Duck
    def nake(self):
        print("ga-ga-ga-ga-ga-ga")

class Ahiru(Tori):#duck
    def tobe(self):
        print("bata-bata-bata-bata-bata")

duck_1go = Duck()
duck_1go.nake()
duck_1go.tobe()

ahiru_1go = Ahiru()
ahiru_1go.nake()
ahiru_1go.tobe()

Step 2, check the correct answer. I wasn't sure, so I asked the wiki for the correct answer.

https://ja.wikipedia.org/wiki/%E3%83%80%E3%83%83%E3%82%AF%E3%83%BB%E3%82%BF%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0

There was a ruby ​​code example on the wiki. I rewrote it to python.


def test(foo):
    foo.sound()
 
class Duck():
    def sound(self):
        print('quack')

class Cat():
    def sound(self):
        print('myaa')

duck_san = Duck()
cat_san = Cat()
test(duck_san)
test(cat_san)

A supplementary explanation for the code on the wiki is one line below.

** Notice that the two classes have no inheritance relationship. ** **

Summary

You can't think of it just by reading a textbook. If you have any comments, please let us know.

Originally, the background and action of this duck typing is meaningful, but after deepening my understanding. .. ..

Recommended Posts

A way to understand Python duck typing
Understand Python's duck typing
A clever way to time processing in Python
A road to intermediate Python
Try to understand Python self
[Python] Another way to import
Probably the easiest way to create a pdf with Python3
A simple way to avoid multiple for loops in Python
A standard way to develop and distribute packages in Python
I created a class in Python and tried duck typing
How to write a Python class
Python ABC-Abstract Classes and Duck Typing
5 Ways to Create a Python Chatbot
Python typing
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
A super introduction to Python bit operations
Send a message from Python to Slack
Analyze stocks to understand a company's performance
I want to build a Python environment
[Python] How to invert a character string
How to get a stacktrace in python
Easy way to use Wikipedia in Python
[Python] Understand how to use recursive functions
Add a Python virtual environment to VSCode
How to run a Maya Python script
What is the fastest way to create a reverse dictionary in python?
Send a message from Slack to a Python server
[Python] List Comprehension Various ways to create a list
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
The fastest way for beginners to master Python
Updated to Python 2.7.9
Send a message to LINE with Python (LINE Notify)
Try to calculate a statistical problem in Python
How to create a Python virtual environment (venv)
How to open a web browser from python
[Python] Road to a snake charmer (4) Tweak Numpy
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
Easy way to use Python 2.7 on Cent OS 6
Let's understand how to pass arguments (Python version)
I want to create a window in Python
The easiest way to synthesize speech with python
I tried playing a typing game in Python
Ported a naive homebrew language compiler to Python
How to create a JSON file in Python
Try to draw a life curve with python
I want to make a game with Python
Python dynamic typing
How to generate a Python object from JSON
Try to make a "cryptanalysis" cipher with Python
How to add a Python module search path
Decide to assign a laboratory with Python (fiction)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
Python Note: When assigning a value to a string
Steps to create a Twitter bot with python
To add a module to python put in Julialang
Introduction to Python with Atom (on the way)