Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the Python duck type

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to define Python duck type". In daily operation, I believe many people have doubts about how to define Python duck type. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to define Python duck type". Next, please follow the editor to study!

A dynamic programming language is a language that can change its structure at run time: for example, new functions, objects, and even code can be introduced, existing functions can be deleted, or other structural changes. At present, dynamic languages such as PHP, Ruby and Python are dynamic languages, while C, C++ and Java are not dynamic languages.

This explanation is very abstract, in fact, dynamic language is relative to static language, the characteristic of static language is that you can know everything from the code before the program is executed, such as the type of variable and the return type of the method:

String s = "hello"

S = "world"

S = 1 / / will report an error when compiling

In a static language, a variable has type information, which is a memory area. The advantage of a static language is that the code structure is very standardized, easy to debug, but sometimes verbose. On the other hand, dynamic languages do not know everything until the program runs, variables (strictly called names, just like human names) do not need to specify types, variables themselves do not have any type information, type information is on the object, what type the object is, we must wait until the program runs to know that the advantage of dynamically typed language is that it is easy to read and does not need to write a lot of type-related code. The disadvantage is that it is not convenient to debug, and when the naming is not standardized, it will cause unreadability, which is not conducive to understanding and so on.

S = "hello"

S = "world"

S = 1 # you can assign values to variables at will, no matter what type it is, it can be duck type.

Duck type is often mentioned in dynamic language. the so-called duck type is: if you walk like a duck and bark like a duck, then it is a duck (If it walks like a duck and quacks like a duck, it must be a duck). Duck type is a design style in dynamically typed language in programming language. the characteristics of an object are not determined by the parent class, but by the methods of the object.

If you are learning static languages such as Java or C++, you may not have a deep understanding of duck types, because the nature of objects in static languages depends on their parent classes. Dynamic languages are different, such as iterators. Any object that implements the _ _ iter__ and _ _ next__ methods can be called an iterator, but the type of the object itself is unlimited and can be customized to any class.

# python3

Class Foo:

Def _ iter__ (self):

Pass

Def _ next__ (self):

Pass

From collections import Iterable

From collections import Iterator

Print (isinstance (Foo (), Iterable)) # True

Print (isinstance (Foo (), Iterator)) # True

We don't need to inherit Iterator to implement iterator functionality. When there is a function that wants to receive a parameter of type Iterator, but we pass an instance object of Foo, it is no problem. In a static language such as Java, we must pass Iterator or its subclass. Duck types usually benefit from "no" test methods and the types of parameters in functions, but rely on documentation, clear code, and tests to ensure proper use. This is both an advantage and a disadvantage, and the disadvantage is that you need to know the parameter type through documentation. To make up for this deficiency, Python3.6 introduces type information, and you can specify the type when defining variables. For example, the following function indicates that it receives a parameter of type str and returns a value of type str:

At this point, the study on "how to define the Python duck type" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report