duck-typing

Duck type testing with C# 4 for dynamic objects

a 夏天 提交于 2019-11-27 12:21:16
I'm wanting to have a simple duck typing example in C# using dynamic objects. It would seem to me, that a dynamic object should have HasValue/HasProperty/HasMethod methods with a single string parameter for the name of the value, property, or method you are looking for before trying to run against it. I'm trying to avoid try/catch blocks, and deeper reflection if possible. It just seems to be a common practice for duck typing in dynamic languages (JS, Ruby, Python etc.) that is to test for a property/method before trying to use it, then falling back to a default, or throwing a controlled

How can I tell if a python variable is a string or a list?

这一生的挚爱 提交于 2019-11-27 12:16:29
I have a routine that takes a list of strings as a parameter, but I'd like to support passing in a single string and converting it to a list of one string. For example: def func( files ): for f in files: doSomethingWithFile( f ) func( ['file1','file2','file3'] ) func( 'file1' ) # should be treated like ['file1'] How can my function tell whether a string or a list has been passed in? I know there is a type function, but is there a "more pythonic" way? Well, there's nothing unpythonic about checking type. Having said that, if you're willing to put a small burden on the caller: def func( *files )

Ruby class types and case statements

只愿长相守 提交于 2019-11-27 10:53:59
What is the difference between case item.class when MyClass # do something here when Array # do something different here when String # do a third thing end and case item.class when MyClass.class # do something here when Array.class # do something different here when String.class # do a third thing end For some reason, the first one of these works sometimes and the second doesn't, and other times, the second one works and the first one doesn't. Why? Which one is the "proper" way to do it? Nakilon You must use: case item when MyClass ... I had the same problem: How to catch Errno::ECONNRESET

What is the difference between polymorphism and duck typing?

感情迁移 提交于 2019-11-27 09:32:27
问题 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

How to handle “duck typing” in Python?

眉间皱痕 提交于 2019-11-27 05:37:49
问题 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

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

自闭症网瘾萝莉.ら 提交于 2019-11-27 04:36:29
问题 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,

Ruby class types and case statements

て烟熏妆下的殇ゞ 提交于 2019-11-27 04:02:16
问题 What is the difference between case item.class when MyClass # do something here when Array # do something different here when String # do a third thing end and case item.class when MyClass.class # do something here when Array.class # do something different here when String.class # do a third thing end For some reason, the first one of these works sometimes and the second doesn't, and other times, the second one works and the first one doesn't. Why? Which one is the "proper" way to do it? 回答1:

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

两盒软妹~` 提交于 2019-11-27 03:31:35
问题 I asked a very similar question yesterday, but it wasn't until today I realised the answer I accepted doesn't solve all my problems. I have the following code: public Expression<Func<TItem, object>> SelectExpression<TItem>(string fieldName) { var param = Expression.Parameter(typeof(TItem), "item"); var field = Expression.Property(param, fieldName); return Expression.Lambda<Func<TItem, object>>(field, new ParameterExpression[] { param }); } Which is used as follows: string primaryKey = _map

How can I tell if a python variable is a string or a list?

纵然是瞬间 提交于 2019-11-26 22:21:45
问题 I have a routine that takes a list of strings as a parameter, but I'd like to support passing in a single string and converting it to a list of one string. For example: def func( files ): for f in files: doSomethingWithFile( f ) func( ['file1','file2','file3'] ) func( 'file1' ) # should be treated like ['file1'] How can my function tell whether a string or a list has been passed in? I know there is a type function, but is there a "more pythonic" way? 回答1: Well, there's nothing unpythonic

Why duck typing is allowed for classes in TypeScript

牧云@^-^@ 提交于 2019-11-26 20:55:27
Looks like in TypeScript it's absolutely fine (from the compiler perspective) to have such code: class Vehicle { public run(): void { console.log('Vehicle.run'); } } class Task { public run(): void { console.log('Task.run'); } } function runTask(t: Task) { t.run(); } runTask(new Task()); runTask(new Vehicle()); But at the same time I would expect a compilation error , because Vehicle and Task don't have anything in common. And sane usages can be implemented via explicit interface definition: interface Runnable { run(): void; } class Vehicle implements Runnable { public run(): void { console