|
Typing
In computer science, duck typing is a form of dynamic typing in which a variable's value itself implicitly determines what the variable can do. more...
Home
Cables, Connectors
Filers, Load Balancers
Home Networking, Cable & DSL
Hubs
KVM Switch Boxes, Cables
Mainframe, DEC, VAX, AS/400
Network Interface Cards,...
Networking, Telecom Tools
Other Networking Equipment
Print Servers, Wired
Racks, Mounts & Patch Panels
Router Components, Memory
Routers, Wired
Security, Firewall, VPN
Server Components, Memory
Servers
Software
Antivirus, Security,...
Apple, Macintosh Software
Business & Productivity
Database & Development Tools
Digital Music & Video...
Downloadable Software
Education & Reference
Arts & Music
Business
Car Repair
College Test Preparation
Computer Related
Dictionaries
Encyclopedias
Britannica
Encarta
Other Encyclopedias
Genealogy
General Reference
Health
History
How-To
Computer Building & Repair
Home Improvement
Music
Other
Photography
Professional, Career
Typing
Web Design
Language
Chinese
English
French
German
Italian
Japanese
Other
Russian
Spanish
Legal and Living Will
Maps, Atlas, Geography
Math
Other
Reading
Religion
Science
Games & Entertainment
Graphics, Photo & Publishing
Handheld Software
Internet Related Utilities
Kids' Software
Networking
Operating Systems
Other Software
Wholesale Lots
Software, Operating Systems
Storage Equipment, NAS, SAN
Switch Components, Memory
Switches
Telephone Systems, Telecom
UPS, Power Protection, APC
Wholesale Lots
Wireless Networking, WiFi
Workstation Components,...
Workstations, Terminals
This implies that an object is interchangeable with any other object that implements the same interface, regardless of whether the objects have a related inheritance hierarchy. Duck typing is a feature of programming languages such as Python, Ruby, and ColdFusion.
The term is a reference to the duck test — "If it walks like a duck and quacks like a duck, it must be a duck". Dave Thomas is thought to have originated the term in the Ruby community.
In Smalltalk any object is interchangeable with any other at runtime. This is the most flexible kind of dynamic typing. Duck typing attempts to limit this flexibility while eliminating a source of possible errors before runtime. The Smalltalk architects sought to achieve true polymorphism at the expense of possible errors creeping in at runtime. In practice, these errors are dealt with, at runtime, via SUnit testing.
Abstract data types are static interfaces that exist only to guarantee, on paper, a particular interface. Smalltalk uses pure dynamic mechanisms, in a variety of ways, to extend the guarantee in practice. This can be as simple as generalizing the "method not found" exception handler into a catch-all lookup mechanism. But it can involve extending the language or environment, as in StrongTalk. Parallels to Smalltalk's exception handling came to be called duck typing in Java and Python), and a single reasonable syntax emerged.
C++ templates implement a static form of duck typing. An iterator, for example, does not inherit its methods from an Iterator base class.
Yet another approach similar to duck typing is OCaml's structural subtyping, where object types are compatible if their method signatures are compatible, regardless of their declared inheritance. This is all detected at compile time through OCaml's type inference system.
In Python
Duck typing is heavily used in Python. The Python Tutorial's Glossary defines duck typing as follows:
Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs hasattr() tests or EAFP (Easier to Ask Forgiveness than Permission) programming.
Read more at Wikipedia.org
|
|