duck-typing

Is this duck-typing in Python?

て烟熏妆下的殇ゞ 提交于 2019-11-28 19:46:10
Here is some Ruby code: class Duck def help puts "Quaaaaaack!" end end class Person def help puts "Heeeelp!" end end def InTheForest x x.help end donald = Duck.new john = Person.new print "Donald in the forest: " InTheForest donald print "John in the forest: " InTheForest john And, I translated it to Python: import sys class Duck: def help(): print("Quaaaaaack!") class Person: def help(): print("Heeeelp!") def InTheForest(x): x.help() donald = Duck() john = Person() sys.stdout.write("Donald in the forest: ") InTheForest(donald) sys.stdout.write("John in the forest: ") InTheForest(john) The

Duck typing, must it be dynamic?

我的未来我决定 提交于 2019-11-28 17:54:18
Wikipedia used to say* about duck-typing : In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. (* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".) It says about structural typing : A structural type system (or property-based type system) is a major class of type system, in which type

How to identify numpy types in python?

柔情痞子 提交于 2019-11-28 17:17:31
问题 How can one reliably determine if an object has a numpy type? I realize that this question goes against the philosophy of duck typing, but idea is to make sure a function (which uses scipy and numpy) never returns a numpy type unless it is called with a numpy type. This comes up in the solution to another question, but I think the general problem of determining if an object has a numpy type is far enough away from that original question that they should be separated. 回答1: Use the builtin type

What is the difference between polymorphism and duck typing?

走远了吗. 提交于 2019-11-28 16:06:44
I'm a little confused with the two terms, here's what I know: Polymorphism is the ability of object of different types to be handled by a common interface. While duck typing, is a kind of dynamic typing that allows objects of different types to respond to the same methods. From what I understand, polymorphism is more about creating an interface that can be shared across different classes. And duck typing is about loose typing that will allow methods to be called as long as it is found on the receiver of the message. Is this correct? I'm pretty confused on the two, they seem related but I do

Does C# have an equivalent to Scala's structural typing?

梦想与她 提交于 2019-11-28 07:01:58
问题 In Scala, I can define structural types as follows: type Pressable = { def press(): Unit } This means that I can define a function or method which takes as an argument something that is Pressable, like this: def foo(i: Pressable) { // etc. The object which I pass to this function must have defined for it a method called press() that matches the type signature defined in the type - takes no arguments, returns Unit (Scala's version of void). I can even use the structural type inline: def foo(i:

How to handle “duck typing” in Python?

放肆的年华 提交于 2019-11-28 05:51:15
I usually want to keep my code as generic as possible. I'm currently writing a simple library and being able to use different types with my library feels extra important this time. One way to go is to force people to subclass an "interface" class. To me, this feels more like Java than Python and using issubclass in each method doesn't sound very tempting either. My preferred way is to use the object in good faith, but this will raise some AttributeErrors . I could wrap each dangerous call in a try/except block. This, too, seems kind of cumbersome: def foo(obj): ... # it should be able to sleep

Consequences of implementing to_int and to_str in Ruby

不羁岁月 提交于 2019-11-28 01:07:38
问题 I have a class which exposes a string value and an int value (a command output and exit code respectively). In addition to exposing them through to_s and to_i , I'm also using to_str and to_int , like so: class Status def to_s @output end alias :to_str :to_s def to_i @status.exitstatus end alias :to_int :to_i end My idea behind this is to be able to use this object in as many situations as possible. having it coerceable to a string or int increases that usability. For instance, I can

Return one of two possible objects of different types sharing a method

谁都会走 提交于 2019-11-27 22:49:09
I have 2 classes: public class Articles { private string name; public Articles(string name) { this.name = name; } public void Output() { Console.WriteLine("The class is: " + this.GetType()); Console.WriteLine("The name is: " + name); } } And public class Questionnaire { private string name; public Questionnaire(string name) { this.name = name; } public void Output() { Console.WriteLine("The class is: " + this.GetType()); Console.WriteLine("The name is: " + name); } } I want to write a method, that takes an integer (1 meaning Articles should be returned, 2 meaning Questionnaire ) and a name.

How can I create a dynamic Select on an IEnumerable<T> at runtime?

本小妞迷上赌 提交于 2019-11-27 13:17:46
问题 Given that I have an IEnumerable<T> , where T is any object, how can I select a specific property from it, given that I know the name of the one of the property names at run time as a string? For example: var externalIEnumerable = DataPassedFromConsumingCode(); // `IEnumerable<T>` string knownPropertyName = "Foo"; var fooSelect = externalIEnumerable.Select(...); In essence, I'm obviously just doing externalIEnumerable.Select(x=> x.Foo); , but I need to perform this Select at runtime, when I

Are there any static duck-typed languages?

这一生的挚爱 提交于 2019-11-27 12:58:37
问题 Can I specify interfaces when I declare a member? After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually work. Why can't predefined classes be bound to an interface at compile time? Example: public interface IMyInterface { public void MyMethod(); } public class MyClass //Does not explicitly implement IMyInterface { public void MyMethod() //But contains a compatible method definition { Console.WriteLine("Hello, world!"); } } ...