KEMBAR78
Programming style - duck type | PPTX
What is -duck typing is a style of dynamic typing in which an object's current set of methods and propertiesdetermines the valid semantics, rather than its inheritance from  - a particular class or implementation of a specific interface.DUCK TYPINGPROGRAMMING STYLEPresented byAbdun Nur Tomal
Define -may be phrased as follows:"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.“In duck typing, one is concerned with just those aspects of an object that are used,
rather than with the type of the object itself. DUCK TYPINGPROGRAMMING STYLE
For example :-in a non-duck-typed language,one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods. In a duck-typed language, >the equivalent function would take an object of any type and >call that object's walk and quack methods. >If the object does not have the methods that are called then the function signals a run-time error. >It is this action of any object having the correct walk and quack methods being accepted by the function that evokesthe quotation and hence the name of this form of typing.DUCK TYPINGPROGRAMMING STYLE
example :-Duck typing is aided by habitually not testing for the type of arguments in method and function bodies,
relying on documentation, clear code, and testing to ensure correct use.
pseudo-code for a duck typed language:function calculate(a, b, c) => return (a+b)*c example1 = calculate (1, 2, 3)example2 = calculate ([1, 2, 3], [4, 5, 6], 2) example3 = calculate ('apples ', 'and oranges, ', 3) printto_string example1 printto_string example2 printto_string example3the result of the code would be:9[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] apples and oranges, apples and oranges, apples and orangesDUCK TYPINGPROGRAMMING STYLE
Thus, duck typing allows polymorphismwithout inheritance.

Programming style - duck type

  • 1.
    What is -ducktyping is a style of dynamic typing in which an object's current set of methods and propertiesdetermines the valid semantics, rather than its inheritance from - a particular class or implementation of a specific interface.DUCK TYPINGPROGRAMMING STYLEPresented byAbdun Nur Tomal
  • 2.
    Define -may bephrased as follows:"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.“In duck typing, one is concerned with just those aspects of an object that are used,
  • 3.
    rather than withthe type of the object itself. DUCK TYPINGPROGRAMMING STYLE
  • 4.
    For example :-ina non-duck-typed language,one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods. In a duck-typed language, >the equivalent function would take an object of any type and >call that object's walk and quack methods. >If the object does not have the methods that are called then the function signals a run-time error. >It is this action of any object having the correct walk and quack methods being accepted by the function that evokesthe quotation and hence the name of this form of typing.DUCK TYPINGPROGRAMMING STYLE
  • 5.
    example :-Duck typingis aided by habitually not testing for the type of arguments in method and function bodies,
  • 6.
    relying on documentation,clear code, and testing to ensure correct use.
  • 7.
    pseudo-code for aduck typed language:function calculate(a, b, c) => return (a+b)*c example1 = calculate (1, 2, 3)example2 = calculate ([1, 2, 3], [4, 5, 6], 2) example3 = calculate ('apples ', 'and oranges, ', 3) printto_string example1 printto_string example2 printto_string example3the result of the code would be:9[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] apples and oranges, apples and oranges, apples and orangesDUCK TYPINGPROGRAMMING STYLE
  • 8.
    Thus, duck typingallows polymorphismwithout inheritance.
  • 9.
    The only requirementthatfunction calculate needs in its variables is having the "+" and the "*" methods.
  • 10.
    The duck testcan be seen in the following example:As far as the function in_the_forest is concerned, the Person object is a duck:class Duck: def quack(self):print("Quaaaaaack!") def feathers(self): print("The duck has white and gray feathers.") class Person: def quack(self): print("The person imitates a duck.") def feathers(self): print("The person takes a feather from the ground and shows it.") def name(self): print("John Smith") defin_the_forest(duck): duck.quack() duck.feathers() def game(): donald = Duck() john = Person() in_the_forest(donald) in_the_forest(john) game()DUCK TYPINGPROGRAMMING STYLE
  • 11.
    This way softwarecan be developed by extending partially working duck typed code.In JavaScriptvar Duck = function () {}; Duck.prototype = { quack: function (){ alert("Quaaaaaack!"); }, feathers: function () { alert("The duck has white and gray feathers."); } }; var Person = function () {}; Person.prototype = { quack: function () { alert("The person imitates a duck."); }, feathers: function () { alert("The person takes a feather from the ground and shows it.");}, name: function () { alert("John Smith"); } }; varin_the_forest = function (duck){ duck.quack(); duck.feathers(); }; var game = function () { vardonald = new Duck(); var john = new Person(); in_the_forest(donald) in_the_forest(john) };game();DUCK TYPINGPROGRAMMING STYLE
  • 12.
    Resources –Wikipediahttp://en.wikipedia.org/wiki/Duck_typing#Concept_examplesDr. Dobb’shttp://drdobbs.com/cpp/184401971http://drdobbs.com/open-sourceBigDingushttp://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/Apache Commonshttp://commons.apache.org/proxy/DUCK TYPINGPROGRAMMING STYLE
  • 13.
    Resources –Wikipediahttp://en.wikipedia.org/wiki/Duck_typing#Concept_examplesDr. Dobb’shttp://drdobbs.com/cpp/184401971http://drdobbs.com/open-sourceBigDingushttp://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/Apache Commonshttp://commons.apache.org/proxy/DUCK TYPINGPROGRAMMING STYLE