duck-typing

When you say Ruby is reflective, does this mainly refer to “duck typing”?

允我心安 提交于 2019-12-04 11:31:13
I was reading a text describing Ruby and it said the following: Ruby is considered a “reflective” language because it’s possible for a Ruby program to analyze itself (in terms of its make-up), make adjustments to the way it works, and even overwrite its own code with other code. I'm confused by this term 'reflective' - is this mainly talking about the way Ruby can look at a variable and figure out whether it's an Integer or a String (duck typing), e.g.: x = 3 x = "three" # Ruby reassigns x to a String type Class reopening is a good example of this. Here's a simple example: class Integer def

Python and dictionary like object

爷,独闯天下 提交于 2019-12-03 10:09:40
I need a python 3.1 deep update function for dictionaries (a function that will recursively update child dictionaries that are inside a parent dictionary). But I think, in the future, my function could have to deal with objects that behave like dictionaries but aren't. And furthermore I want to avoid using isinstance and type (because they are considered bad, as I can read on almost every Pythonista's blog). But duck typing is part of Python's philosophy, so how could I check if an object is dictionary-like? Thanks! Edit : Thank all for the answers. Just in case, the function I coded can be

'from X import a' versus 'import X; X.a'

三世轮回 提交于 2019-12-03 09:40:44
问题 I've seen some Python programmers use the following style fairly consistently (we'll call it style 1): import some_module # Use some_module.some_identifier in various places. For support of this style, you can cite the "explicit is better than implicit" maxim. I've seen other programmers use this style (style 2): from some_module import some_identifier # Use some_identifier in various places. The primary benefit that I see in style 2 is maintainability -- especially with duck typing ideals I

If duck-typing in Python, should you test isinstance?

南笙酒味 提交于 2019-12-03 08:51:13
问题 You have a Python class which needs an equals test. Python should use duck-typing but is it (better/more accurate) to include or exclude an isinstance test in the eq function? For example: class Trout(object): def __init__(self, value): self.value = value def __eq__(self, other): return isinstance(other, Trout) and self.value == other.value 回答1: Using isinstance in __eq__ methods is pretty common. The reason for this is that if the __eq__ method fails, it can fallback on an __eq__ method from

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

烂漫一生 提交于 2019-12-03 05:15:34
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? 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 into doing other things, but genericity was the original intention. Does it mean all generic types referenced

Should I define interfaces in Duck Typed languages?

喜夏-厌秋 提交于 2019-12-03 03:00:59
I'm just about to write my first application in a duck typed language (Groovy). If I was to write the same application in a static typed language then I would need to define some interfaces. Obviously because of the duck typing in Groovy they are not actually required. At the moment I am thinking that it might make sense to define them anyway as documentation of the methods that need to be implemented in the various objects. Am I missing the point? I've been reading on this recently here on SO (and I can't find the link right now, but it's one of those "why are dynamic languages good?" posts,

'from X import a' versus 'import X; X.a'

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:13:40
I've seen some Python programmers use the following style fairly consistently (we'll call it style 1): import some_module # Use some_module.some_identifier in various places. For support of this style, you can cite the "explicit is better than implicit" maxim. I've seen other programmers use this style (style 2): from some_module import some_identifier # Use some_identifier in various places. The primary benefit that I see in style 2 is maintainability -- especially with duck typing ideals I may want to swap some_module for some_other_module. I also feel style 2 wins points with the

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

∥☆過路亽.° 提交于 2019-12-02 14:22:44
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 ducking the issue by refusing to do the research, but I am quacking up at all the flocking hype i've

Checking existence of properties in JavaScript

大兔子大兔子 提交于 2019-12-02 00:55:24
问题 I'm new to JavaScript and a little bit confused with the duck typing concept. As far as I can tell, I understood the concept. But that leads to a strange consequence in my thoughts. I will explain with the following example: I'm currently working on a mobile web app with jQuery Mobile. At one point I capture the vmousedown event for a canvas. I'm interested in the pressure of the touch. I found the Touch.webkitForce property. $('#canvas').live('vmousedown', function(e){ console.log(e

In Scala, why can't I implement a trivial generic function like this?

爱⌒轻易说出口 提交于 2019-12-01 23:57:11
问题 I want a generic function called "double", which behaves like this and could be applied to any type with def +(x:T):T method: double("A") > "AA" double(1) > 2 double(0.2) > 0.4 So I write this function like this: def double[T](x:T):T = { x+x } But when I run it in REPL, scala compains about it: scala> def double[T](x:T):T = { x+x } <console>:7: error: type mismatch; found : T required: String def double[T](x:T):T = { x+x } ^ I think structural type may be an approach to implement duck typing,