duck-typing

How is duck typing different from the old 'variant' type and/or interfaces?

烈酒焚心 提交于 2019-12-20 08:09:17
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

How is duck typing different from the old 'variant' type and/or interfaces?

不打扰是莪最后的温柔 提交于 2019-12-20 08:08:12
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

Why Collection Initialization Throws NullReferenceException

僤鯓⒐⒋嵵緔 提交于 2019-12-20 02:13:21
问题 The following code throws a NullReferenceException : internal class Foo { public Collection<string> Items { get; set; } // or List<string> } class Program { static void Main(string[] args) { new Foo() { Items = { "foo" } // throws NullReferenceException }; } } Why don't collection initiliazers work in this case, although Collection<string> implements the Add() method, and why is NullReferenceException is thrown? Is it possible to get the collection initializer working, or is Items = new

How to add a dataclass field without annotating the type?

半世苍凉 提交于 2019-12-19 14:58:31
问题 When there is a field in a dataclass for which the type can be anything, how can you omit the annotation? @dataclass class Favs: fav_number: int = 80085 fav_duck = object() fav_word: str = 'potato' It seems the code above doesn't actually create a field for fav_duck . It just makes that a plain old class attribute. >>> Favs() Favs(fav_number=80085, fav_word='potato') >>> print(*Favs.__dataclass_fields__) fav_number fav_word >>> Favs.fav_duck <object at 0x7fffea519850> 回答1: The dataclass

Does LINQ “Query Syntax” Support Duck Typing?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 11:55:34
问题 Regarding LINQ query syntax... var foo = new List<int> { 1, 2 }; var boo = from n in foo where n > 1 select n; ...I always thought this syntax was limited to operating on IEnumerable. Or at least until I learned about IQueryable. And perhaps IObservable as well. But I recently noticed a suggestion that query syntax is based on duck typing. That story didn't look terribly convincing, until I found a site that is dedicated to LINQ to Tasks. LINQ to Tasks looks like it is wholly dependent on

Duck typing and class methods (or, how to use a method from both a class and an instance?)

折月煮酒 提交于 2019-12-19 04:31:11
问题 I have some code which I would like to pass instances or classes interchangeably. All I will do in that code is to call a method that I expect both classes and instances to have (the method go() in the example below). Unfortunately, I can't create a classmethod with the same name of a regular method... See example below. I initially expected the second call to produce an a instead of a b . Any advice on how to achieve this? Type "help", "copyright", "credits" or "license" for more information

Uses for Dynamic Languages

喜欢而已 提交于 2019-12-18 15:54:15
问题 My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent compile-time duck typing), I'm curious what the benefits are of dynamic languages even when you have those. The bottom line is that, if I'm going to learn Python, I want to learn it

What's the relationship between C++ template and duck typing?

时光怂恿深爱的人放手 提交于 2019-12-18 12:45:06
问题 To me, C++ template used the idea of duck typing, is this right? Does it mean all generic types referenced in template class or method are duck type? 回答1: To me, C++ template used the idea of duck typing, is this right? No, C++ templates are used to implement generic code. That is, if you have code that can work with more than one type, you don't have to duplicate it for each type. Things like std::vector and std::list are obvious examples of this in action. C++ templates have been abused

How excess property check helps?

我的梦境 提交于 2019-12-18 08:57:46
问题 For the below code, interface SquareConfig{ color?: string; width?: number; } interface Square{ color: string; area: number; } function createSquare(config: SquareConfig): Square { let newSquare:Square = {color: "white", area: 100}; if (config.color) { newSquare.color = config.color; } if (config.width) { newSquare.area = config.width * config.width; } return newSquare; } below argument( myObj ) inferred as type any is allowed to pass as argument by type checker at compile time. JS code use

issubclass of abstract base class Sequence

余生颓废 提交于 2019-12-18 08:57:30
问题 This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__ , __len__ , __contains__ , __iter__ , __reversed__ , index , and count . So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self