In programming, duck typing (English: duck typing) is a style of dynamic typing. In this style, the effective semantics of an object are determined not by inheriting from a specific class or implementing a specific interface, but by the current set of methods and properties. The name of this concept comes from the duck test proposed by James Whitcomb Riley. The "duck test" can be expressed like this: "When you see a bird that walks like a duck, swims like a duck, and quacks like a duck, then this bird It can be called a duck." In duck typing, the focus is not on the type of the object itself, but on how it is used. For example, in a language that doesn't use duck typing, we could write a function that takes an object of type duck and calls its walk and bark methods. In a language that uses duck typing, such a function can accept an object of any type and call its walk and call methods. If the methods that need to be called do not exist, a runtime error will be raised. The fact that any object with the correct walk and call methods can be accepted by a function leads to the above statement, hence the name of this way of determining types. Duck typing is usually
1. Detailed explanation of duck typing duck typing programming and Python implementation
Introduction: This article mainly introduces the detailed explanation of duck typing duck typing programming and Python implementation examples. Duck typing specifically refers to a programming style in interpreted languages. Friends who need it can refer to it. Next
2. Introducing Python’s duck typing in detail
##Introduction: I believe that Python developers are familiar with Python's duck typing. The accurate definition of duck typing in Wikipedia is 'a style of dynamic typing. In this style, the effective semantics of an object are not determined by inheriting from a specific class or implementing a specific interface, but by the "current set of methods and properties". So this article gives you the duck typing of Python.
3. Should a Pythonic class check parameter validity in __init__?
#Introduction: I know that according to the duck typing principle, parameter types should not be checked. But should parameters be checked for validity in other ways? For example: class Comment(object):... def __init__(self, content, creater, ipaddress):... ... self.content = content... ... self.creater = creater... .. .self.ipaddre
The above is the detailed content of Detailed explanation of duck typing in Python. For more information, please follow other related articles on the PHP Chinese website!