duck-typing

Is this duck-typing in Python?

╄→гoц情女王★ 提交于 2020-01-09 13:58:33
问题 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

Is this duck-typing in Python?

亡梦爱人 提交于 2020-01-09 13:56:07
问题 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

How do I argue against Duck-typing in a strongly typed language like Java?

青春壹個敷衍的年華 提交于 2020-01-03 10:41:32
问题 I work on a team of Java programmers. One of my co-workers suggests from time-to-time that I do something like "just add a type field" (usu. "String type"). Or code will be committed laden with " if (foo instanceof Foo){...} else if( foo instanceof Bar){...} ". Josh Bloch's admonition that "tagged classes are a wan imitation of a proper class hierarchy" notwithstanding, what is my one-line response to this sort of thing? And then how do I elaborate the concept more seriously? It's clear to me

How to document a duck type?

為{幸葍}努か 提交于 2020-01-03 07:00:27
问题 I'm having documentation bloat on me, as anytime I encounter a complex duck-type, I need some way to say "this duck type" but instead get caught in an endless cycle of "your function requires this of this input, but doesn't document it", and then documenting it. This results in bloated, repetitive documentation, such as the following: def Foo(arg): """ Args: arg: An object that supports X functionality, and Y functionality, and can be passed to Z other functionality. """ # Insert code here.

Using LINQ, is it possible to output a dynamic object from a Select statement? If so, how?

孤街浪徒 提交于 2020-01-02 00:53:07
问题 Presently in LINQ, the following compiles and works just fine: var listOfFoo = myData.Select(x => new FooModel{ someProperty = x.prop1, someOtherProperty = x.prop2 }); public class FooModel{ public string someProperty { get; set; }; public string someOtherProperty { get; set; }; } However, the past few versions of .NET/C# have expanded the role of dynamic objects such as the ExpandoObject and I am wondering if there is a way to basically do this: var listOfFoo = myData.Select(x => new

String '00' equals '.0' in Coldfusion ? What else?

雨燕双飞 提交于 2019-12-24 00:37:56
问题 So I noticed that ColdFusion returns true for the condition '00' == '.0' . This is reproducible in CF 9.1 and CF 10. I could easily work around this by adding a find('.', foo) condition, but this is NOT normal. Things like this make me doubt ColdFusion. It makes me wonder why it returns true on this clearly false condition, and what other values would it consider equal? Is there a list of values that you can't compare in Coldfusion? Or better yet, is there a solid solution to prevent this

boolean subtract DeprecationWarning

若如初见. 提交于 2019-12-23 09:07:09
问题 I recently upgraded to numpy 1.9dev. (For improved OpenBlas support). I have some code that does x-y Where x and y are samples from a probability distribution. If the distribution is Bernoulli, then they are boolean. If the distribution is Gaussian, then they are floats. where depending on the path followed x and y might be bools or floats. I don't have to care as python has duck-typing. If it can subtract then it is a valid value for x and y I get this warning: DeprecationWarning: numpy

How to Work with Ruby Duck Typing

主宰稳场 提交于 2019-12-22 04:28:29
问题 I am learning Ruby and I'm having a major conceptual problem concerning typing. Allow me to detail why I don't understand with paradigm. Say I am method chaining for concise code as you do in Ruby. I have to precisely know what the return type of each method call in the chain, otherwise I can't know what methods are available on the next link. Do I have to check the method documentation every time?? I'm running into this constantly running tutorial exercises. It seems I'm stuck with a process

Python and dictionary like object

不羁的心 提交于 2019-12-21 03:39:14
问题 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

Should I define interfaces in Duck Typed languages?

元气小坏坏 提交于 2019-12-20 11:56:12
问题 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? 回答1: I've been reading on this recently here