"Duck type" in dynamic language, static language and dynamic language

Source: Internet
Author: User

Dynamic languages and Static languages:

The so-called dynamic and Static, refers to the type of program. Type is also defined as class,int, such as the type of the variable, the type of the function's entry, and the return value type. Dynamic type language, which means that there is no need to make a type when writing a program, a runtime check, or a static type language, which means that when writing a program, you need to explicitly develop various types that are checked at compile time.

Some people say that a static type language is called a strongly typed language, which is a language that requires the declaration of a variable/object type, and in general it needs to be compiled and executed.

such as c/c++/java/c#

Some people say that a dynamic type language is called a weakly typed language, which is a language that does not require a variable/object type declaration, and generally does not need to be compiled (but also compiled).

such as Php/asp/ruby/python/perl/abap/sql/javascript/unix shell, etc.

Python is a dynamically typed language, and the official argument is the dynamic Typing.

Take Python For example: A variable in a dynamic language does not need to be declared, the assignment does not require a type, and at any time it can change the type it points to, the return value of the function is not fixed, and the different entry parameters can be different types of return values.

"Duck type" for dynamic language:

What is the "duck type" of a dynamic language?

For example, we wrote a function Test_duck (), the parameter of the function is a specific type of object (in this case, the animal type), the function is to do one thing, is called the object's Run () function. Assuming this type is dog,dog inherited animal, now we have two objects, dog and animal, and the ratio is type dog and animal.

If we pass a and B as input into the Test_duck (), both the dog and the animal have the run () member function, and the execution is OK.

At this point, if we define a type again, Test,test has nothing to do with animal and dog, and the only connection is that test has a run () function. Suppose that test is an object of test, and we pass the test to Test_duck (), it can be executed as well.

We originally designed the test_duck () function to pass in the objects of dog and animal, or to inherit the other new type objects of the dog and animal, but suddenly there was a test that had nothing to do with dog and animal, The object of test can also pass in the Test_duck () function. We say test is "duck type" (A-like object).

classAnimal (object):defRun (self):Print('Animal is running')classDog (Animal):defRun (self):Print('Dog is running')classTest (Animal):defRun (self):Print('Test is right')defTest_duck (animal): Animal.run () Dog=Dog () dog.run () test=test () test_duck (test)

The result of the operation is:

================== restart:e:/python_pro/lxf/class_run.py ==================  is  is Right

This is the "duck type" of dynamic language, it does not require strict inheritance system, an object as long as "look like a duck, walk like a duck", it can be regarded as a duck.

Python's "File-like object" is a type of duck. For a real file object, it has a read () method that returns its contents. However, many objects, as long as there is a read () method, are considered "File-like object". Many functions receive parameters that are "File-like object", you do not have to pass in a real file object, you can pass in any object that implements the Read () method.

"Duck type" in dynamic language, static language and dynamic language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.