What is -<br />duck typing is a style of dynamic typing in which <br /><ul><li>an object's current set of methods </li></u...
Define -<br />may be phrased as follows:<br />"When I see a bird that walks like a duckand swims like a duck and quacks li...
rather than with the type of the object itself.</li></ul> <br />DUCK TYPING<br />PROGRAMMING STYLE<br />
For example :-<br /><ul><li>in a non-duck-typed language,</li></ul>one can create a function that takes an object of type ...
example :-<br /><ul><li>Duck typing is aided by habitually not testing for the type of arguments in method and function bo...
relying on documentation, clear code, and testing to ensure correct use.
pseudo-code for a duck typed language:</li></ul>function calculate(a, b, c) => return (a+b)*c <br />example1 = calculate (...
<ul><li>Thus, duck typing allows polymorphismwithout inheritance.
Upcoming SlideShare
Loading in...5
×

Programming style - duck type

417

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
417
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Programming style - duck type

  1. 1. What is -<br />duck typing is a style of dynamic typing in which <br /><ul><li>an object's current set of methods </li></ul>and <br /><ul><li>propertiesdetermines the valid semantics, </li></ul>rather than its inheritance from - <br /><ul><li>a particular class or implementation of a </li></ul>specific interface.<br />DUCK TYPING<br />PROGRAMMING STYLE<br />Presented by<br />Abdun Nur Tomal<br />
  2. 2. Define -<br />may be phrased as follows:<br />"When I see a bird that walks like a duckand swims like a duck and quacks like a duck, I call that bird a duck.“<br />In duck typing, one is concerned with just those aspects of an object that are <br /><ul><li>used,
  3. 3. rather than with the type of the object itself.</li></ul> <br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  4. 4. For example :-<br /><ul><li>in a non-duck-typed language,</li></ul>one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods. <br /><ul><li>In a duck-typed language, </li></ul>>the equivalent function would take an object of any type and <br />>call that object's walk and quack methods. <br />>If the object does not have the methods that are called then the function signals a run-time error. <br />>It is this action of any object having the correct walk and quack methods being accepted by the function that evokes<br />the quotation and hence the name of this form of typing.<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  5. 5. example :-<br /><ul><li>Duck typing is aided by habitually not testing for the type of arguments in method and function bodies,
  6. 6. relying on documentation, clear code, and testing to ensure correct use.
  7. 7. pseudo-code for a duck typed language:</li></ul>function calculate(a, b, c) => return (a+b)*c <br />example1 = calculate (1, 2, 3)<br />example2 = calculate ([1, 2, 3], [4, 5, 6], 2) <br />example3 = calculate ('apples ', 'and oranges, ', 3) <br />printto_string example1 <br />printto_string example2 <br />printto_string example3<br /><ul><li>the result of the code would be:</li></ul>9<br />[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] <br />apples and oranges, apples and oranges, apples and oranges<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  8. 8. <ul><li>Thus, duck typing allows polymorphismwithout inheritance.
  9. 9. The only requirement thatfunction calculate needs in its variables is having the "+" and the "*" methods.
  10. 10. The duck test can be seen in the following example:</li></ul>As far as the function in_the_forest is concerned, the Person object is a duck:<br />class Duck: <br />def quack(self):<br />print("Quaaaaaack!") <br />def feathers(self): <br />print("The duck has white and gray feathers.") <br />class Person: <br />def quack(self): <br />print("The person imitates a duck.") <br />def feathers(self): <br />print("The person takes a feather from the ground and shows it.") <br />def name(self): <br />print("John Smith") <br />defin_the_forest(duck): <br />duck.quack() <br />duck.feathers() <br />def game(): <br />donald = Duck() <br />john = Person() <br />in_the_forest(donald) <br />in_the_forest(john) <br />game()<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  11. 11. This way software can be developed by extending partially working duck typed code.<br /><ul><li>In JavaScript</li></ul>var Duck = function () {}; <br />Duck.prototype = { <br />quack: function (){ alert("Quaaaaaack!"); }, <br />feathers: function () { alert("The duck has white and gray feathers."); } <br />}; <br />var Person = function () {}; <br />Person.prototype = { <br />quack: function () { alert("The person imitates a duck."); }, <br />feathers: function () { alert("The person takes a feather from the ground and shows it.");}, <br />name: function () { alert("John Smith"); } <br />}; <br />varin_the_forest = function (duck)<br />{ <br />duck.quack(); <br />duck.feathers(); <br />}; <br />var game = function () <br />{ <br />vardonald = new Duck(); <br />var john = new Person(); <br />in_the_forest(donald) <br />in_the_forest(john) <br />};<br />game();<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  12. 12. Resources –<br />Wikipedia<br />http://en.wikipedia.org/wiki/Duck_typing#Concept_examples<br />Dr. Dobb’s<br />http://drdobbs.com/cpp/184401971<br />http://drdobbs.com/open-source<br />Big Dingus<br />http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/<br />Apache Commons<br />http://commons.apache.org/proxy/<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  13. 13. Resources –<br />Wikipedia<br />http://en.wikipedia.org/wiki/Duck_typing#Concept_examples<br />Dr. Dobb’s<br />http://drdobbs.com/cpp/184401971<br />http://drdobbs.com/open-source<br />Big Dingus<br />http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/<br />Apache Commons<br />http://commons.apache.org/proxy/<br />DUCK TYPING<br />PROGRAMMING STYLE<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×